首页 > 代码库 > Android之动态图片

Android之动态图片

在Android中,比起静态图片来动态图片会更加生动更加酷炫,因为这种视觉效果,你应该会发现我们手机中大多数应用软件的导航页面也都是采用动态图片来展示。动态图片的格式有gif、png格式等等。

我们就来看看动态图片,我们可以先自定义SurfaceView视图,然后在MainActivity中添加显示方法就行了。

MainActivity:

package com.cdp.dongtaitupian;;import android.app.Activity;import android.os.Bundle;import android.view.Window;import android.view.WindowManager;public class MainActivity extends Activity {	@Override	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		//设置全屏		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);		requestWindowFeature(Window.FEATURE_NO_TITLE);		//显示自定义的SurfaceView视图		setContentView(new MySurfaceView(this));	}}

SurfaceView:

package com.cdp.dongtaituian;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.view.KeyEvent;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.view.SurfaceHolder.Callback;/** *  * @author Himi * */public class MySurfaceView extends SurfaceView implements Callback, Runnable {	private SurfaceHolder sfh;	private Paint paint;	private Thread th;	private boolean flag;	private Canvas canvas;	//声明一张图片	private Bitmap bmp;	//声明图片的X,Y坐标	private int bmpX, bmpY;	/**	 * SurfaceView初始化函数	 */	public MySurfaceView(Context context) {		super(context);		sfh = this.getHolder();		sfh.addCallback(this);		paint = new Paint();		paint.setColor(Color.WHITE);		paint.setAntiAlias(true);		setFocusable(true);	}	/**	 * SurfaceView视图创建,响应此函数	 */	@Override	public void surfaceCreated(SurfaceHolder holder) {		bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.water);		//让位图初始化X坐标正好充满屏幕		bmpX = -bmp.getWidth()+this.getWidth();		//让位图绘制在的最下方,且图片Y坐标正好是(屏幕高-图片高)		bmpY = this.getHeight() - bmp.getHeight();		flag = true;		//实例线程		th = new Thread(this);		//启动线程		th.start();	}	public void myDraw() {		try {			canvas = sfh.lockCanvas();			if (canvas != null) {				canvas.drawColor(Color.WHITE);				canvas.drawBitmap(bmp, bmpX, bmpY, new Paint());			}		} catch (Exception e) {		} finally {			if (canvas != null)				sfh.unlockCanvasAndPost(canvas);		}	}	/**	 * 触屏事件监听	 */	@Override	public boolean onTouchEvent(MotionEvent event) {		return true;	}	/**	 * 按键事件监听	 */	@Override	public boolean onKeyDown(int keyCode, KeyEvent event) {		return super.onKeyDown(keyCode, event);	}	private void logic() {		bmpX+=5;	}	@Override	public void run() {		while (flag) {			long start = System.currentTimeMillis();			myDraw();			logic();			long end = System.currentTimeMillis();			try {				if (end - start < 50) {					Thread.sleep(50 - (end - start));				}			} catch (InterruptedException e) {				e.printStackTrace();			}		}	}	/**	 * SurfaceView视图状态发生改变,响应此函数	 */	@Override	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {	}	/**	 * SurfaceView视图消亡时,响应此函数	 */	@Override	public void surfaceDestroyed(SurfaceHolder holder) {		flag = false;	}}

Android之动态图片