android 动态背景的实现以及Su易做图ceView中添加EditText控件
首先还是一贯作风,我们先看案例: 静态图看不出来效果,如果用过此软件(扎客)的同学们都知道,她的背景会动.怎么样,是不是觉得很时尚,起码比静态的要好(个人观点).其实实现起来并不复杂,这个如果让做游戏程序员做简直太小儿科了,这里我说明一点,其实我们做应用的也应该多少了解下游戏编程思维,起码对我们做应用有很好的帮助.
下面我简单介绍下实现方式.
实现原理:自定义一个Su易做图ceView控件.对之不停的onDraw,使得其背景动起来.
对于Su易做图ceView如果不了解的同学们麻烦你先上网查找下,网上相关介绍很多.
这里我简单介绍下其功能:首先这个控件是View的子类.好处就是可以在线程中(非UI线程)对UI进行更新.
MySu易做图ceView.java
[java]
package com.jj.dynamic;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.Su易做图ceHolder;
import android.view.Su易做图ceView;
import android.view.Su易做图ceHolder.Callback;
public class MySu易做图ceView extends Su易做图ceView implements Callback, Runnable {
private Context mContext;
private Su易做图ceHolder su易做图ceHolder;
private boolean flag = false;// 线程标识
private Bitmap bitmap_bg;// 背景图
private float mSu易做图ceWindth, mSu易做图ceHeight;// 屏幕宽高
private int mBitposX;// 图片的位置
private Canvas mCanvas;
private Thread thread;
// 背景移动状态
private enum State {
LEFT, RINGHT
}
// 默认为向左
private State state = State.LEFT;
private final int BITMAP_STEP = 1;// 背景画布移动步伐.
public MySu易做图ceView(Context context, AttributeSet attrs) {
super(context, attrs);
flag = true;
this.mContext = context;
setFocusable(true);
setFocusableInTouchMode(true);
su易做图ceHolder = getHolder();
su易做图ceHolder.addCallback(this);
}
/***
* 进行绘制.
*/
protected void onDraw() {
drawBG();
updateBG();
}
/***
* 更新背景.
*/
public void updateBG() {
/** 图片滚动效果 **/
switch (state) {
case LEFT:
mBitposX -= BITMAP_STEP;// 画布左移
break;
case RINGHT:
mBitposX += BITMAP_STEP;// 画布右移
break;
default:
break;
}
if (mBitposX <= -mSu易做图ceWindth / 2) {
state = State.RINGHT;
}
if (mBitposX >= 0) {
state = State.LEFT;
}
}
/***
* 绘制背景
*/
public void drawBG() {
mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);// 清屏幕.
mCanvas.drawBitmap(bitmap_bg, mBitposX, 0, null);// 绘制当前屏幕背景
}
@Override
public void run() {
while (flag) {
synchronized (su易做图ceHolder) {
mCanvas = su易做图ceHolder.lockCanvas();
onDraw();
su易做图ceHolder.unlockCanvasAndPost(mCanvas);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@Override
public void su易做图ceCreated(Su易做图ceHolder holder) {
mSu易做图ceWindth = getWidth();
mSu易做图ceHeight = getHeight();
int mWindth = (int) (mSu易做图ceWindth * 3 / 2);
/***
* 将图片缩放到屏幕的3/2倍.
*/
bitmap_bg = BitmapUtil.ReadBitmapById(mContext, R.drawable.hypers_bg,
(int) mWindth, (int) mSu易做图ceHeight);
thread = new Thread(this);
thread.start();
}
&n
补充:移动开发 , Android ,