只用一张图片,将图片的每一点的像素信息保存到数组中,每一点像素是ARGB的方式,正好32位,放到一个int类型的值中。然后改变int值的高八位的大小,实现对alpha值的改变。在将改变的数组信息创造一张新的图片就可以了。
package liu.com.kiexun; import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message; public class SimpleTestActivity extends Activity { /** Called when the activity is first created. */ SimpleFlash simpleFlash; boolean flag=true; private Handler handler=new Handler() { public void handleMessage(Message msg) { switch(msg.what) { case 1: if (flag) { try { Thread.sleep(1000);//第一张图片时间显示为1秒 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } flag=false; } simpleFlash.invalidate(); break; default: break; } }; }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //在这里才产生contex,才可以对view进行初始化 simpleFlash=new SimpleFlash(this,handler); setContentView(simpleFlash); }}
package liu.com.kiexun;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.os.Handler;import android.os.Message;import android.view.MotionEvent;import android.view.View; public class SimpleFlash extends View{ int index=0; int size=480*800; Bitmap firstBitmap,secondBitmap; Canvas canvas=null; int pixels[]=new int[size]; Handler handler ; int changeArrary[]= { (1<<31)-1, (1<<30)-1, (1<<29)-1, (1<<28)-1, (1<<27)-1, (1<<26)-1, (1<<25)-1, (1<<24)-1 }; int changeArrary2[]={/* 11111110 11111100 11111000 11110000 11100000 11000000 10000000 01111111 00111111 00011111 00001111 00000111 00000011 00000001 00000000 1<<32 相当于没有进行移位 >=32位的时候与移的为数是与32的余数 */ ( ( (1<<31)-1 )+(1<<31)-(1<<24) ), ( ( (1<<31)-1 )+(1<<31)-(1<<24)-(1<<25) ), ( ( (1<<31)-1 )+(1<<31)-(1<<24)-(1<<25)-(1<<26) ), ( ( (1<<31)-1 )+(1<<31)- (1<<24)-(1<<25)-(1<<26)-(1<<27)), ( ( (1<<24)-1 )+(1<<31)+(1<<30)+(1<<29)), ( ( (1<<24)-1 )+(1<<31)+(1<<30)), ( ( (1<<24)-1 )+(1<<31)), (1<<31)-1, (1<<30)-1, (1<<29)-1, (1<<28)-1, (1<<27)-1, (1<<26)-1, (1<<25)-1, (1<<24)-1 }; public SimpleFlash(Context context,Handler handler) { super(context); this.handler=handler; // TODO Auto-generated constructor stub firstBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.about); secondBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.help); firstBitmap.getPixels(pixels, 0, 480, 0, 0, 480, 800); } /* * (non-Javadoc) * @see android.view.View#onDraw(android.graphics.Canvas) * draw函数执行完毕才能显示出图片,应该是执行完毕后才能提交绘画消息 */ public void onDraw(Canvas canvas) { this.canvas=canvas; canvas.drawBitmap(secondBitmap, 0, 0, null); /* * 不会先显示第二个图片,5秒后在显示第一个图片 */ firstBitmap=Bitmap.createBitmap(pixels, 480, 800,Config.ARGB_8888); canvas.drawBitmap(firstBitmap, 0, 0, null); changePixels(); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void changePixels() { if (index<8) { for (int i = 1; i < pixels.length; i++) { pixels[i]=pixels[i]&changeArrary[index]; } index++; Message changeMessage=new Message(); changeMessage.what=1; handler.sendMessage(changeMessage); } } }