如果你有一张图片,想实现把图片中某一类的像素点修改一下,例如:把一个图片中的全白的像素点改成你想要的颜色,可以通过如下方法实现
第一步:创建一个布局
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
android:id="@+id/image1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#0000FF"
/>
android:id="@+id/image2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
第二步:java程序
public class MyBitmapTestActivity extends Activity {
private ImageView image1;
private ImageView image2;
int m_BmpPixel[];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);
image1.setImageResource(R.drawable.image);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.image);
Bitmap bitmap2 = transparentImage(bitmap);
image2.setImageBitmap(bitmap2);
}
public Bitmap transparentImage(Bitmap bmp) {
int m_ImageWidth, m_ImageHeigth;
m_ImageWidth = bmp.getWidth();
m_ImageHeigth = bmp.getHeight();
m_BmpPixel = new int[m_ImageWidth * m_ImageHeigth];
bmp.getPixels(m_BmpPixel, 0, m_ImageWidth, 0, 0, m_ImageWidth,
m_ImageHeigth);
System.out.println("AAAAAAAAAAAA2");
for (int i = 0; i < m_ImageWidth * m_ImageHeigth; i++) {
if ((m_BmpPixel[i] & 0x00ffffff) == 0x00ffffff) {
m_BmpPixel[i] = 0x7f0000ff;
}
}
System.out.println("AAAAAAAAAAAA3");
bmp.setPixels(m_BmpPixel, 0, m_ImageWidth, 0, 0, m_ImageWidth,
m_ImageHeigth);
System.out.println("AAAAAAAAAAAA4");
return bmp;
}
}
运行即得,这里不再多说。