android 显示16色的图片:输入用颜色矩阵,显示对应的16色位图
参考资料:http://blog.sina.com.cn/s/blog_4b650d650100nq9g.html
http://www.chalayout.com/Article/%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1/200603/20060301114940.html
标准的16色的调色板如下:
颜色RGB-----------------------
0000
100170
201700
30170170
417000
51700170
61701700
7170170170
8858585
98585255
108525585
1185255255
122558585
1325585255
1425525585
15255255255
问题背景:输入参数格式为一串byte流:heigth,width,颜色像素数据。颜色像素数据是每4bit一个像素,16色。
运行结果:如下的一小块彩方块。
源码如下:
package android.test.showpic;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.graphics.Bitmap.Config;
public class ShowpicActivity extends Activity {
/** Called when the activity is first created. */
ImageView img;
final String tag="mayShowPic";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findviews();
int[] colorindex=new int[]{
0xff000000,0xff0000aa,0xff00aa00,0xff00aaaa,0xffaa0000,
0xffaa00aa,0xffaaaa00,0xffaaaaaa,0xff555555,0xff5555ff,
0xff55ff55,0xff55ffff,0xffff5555,0xffff55ff,0xffffff55,
0xffffffff
};
int h=23,w=33,i;
int lenold=((h*w)+1)/2,lennew=h*w;
byte[] colorold=new byte[lenold];
for(i=0;i<lenold;i++)
{
colorold[i]=(byte)(i%256);
}
int[] colornew=new int[lennew];
int hi,lo;
for(i=0;i<lennew-2;)
{
hi=((int)colorold[i>>1]&0xf0)>>4;
lo=((int)colorold[i>>1]&0x0f);
Log.i(tag, "i,colorold,hi,lo"+i+","+colorold[i>>1]+","+hi+","+lo);
colornew[i]=colorindex[hi];
colornew[i+1]=colorindex[lo];
i=i+2;
}
hi=((int)colorold[lenold-1]&0xf0)>>4;
lo=((int)colorold[lenold-1]&0x0f);
Log.i(tag, ""+colorold[lenold-1]+","+hi+","+lo);
colornew[i]=colorindex[hi];
if(i+1<lennew)
colornew[i+1]=colorindex[lo];
i=i+2;
Log.i(tag, "aa");
Bitmap b = Bitmap.createBitmap(colornew,w, h,Bitmap.Config. ARGB_8888 );
Log.i(tag, "bb");
ImageView img23 = new ImageView(ShowpicActivity.this);
Log.i(tag, "anew");
img.setImageBitmap(b);
Log.i(tag, "bb2");
}
void findviews()
{
img=(ImageView)findViewById(R.id.img);
}
}
更多相关文章
- 一句话锁定MySQL数据占用元凶
- Context和SQLiteOpenHelper创建数据库
- Android(安卓)sqlite3插入速度优化方案
- Button 或 ImageButton 背景设为 透明 或半透明
- Android数据的四种存储方式之SQLite数据库
- Android(安卓)动态修改SVG数据
- Android异步任务处理之AsyncTaskLoader的使用
- Android(安卓)Sqlite数据库详解
- Android(安卓)Camera 二 JNI JAVA和C/CPP图像数据传输流程分析