80,350
社区成员
发帖
与我相关
我的任务
分享
import android.view.SurfaceView;
import android.view.View;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
public class MySurfaceView extends SurfaceView implements Callback {
private SurfaceHolder sfh;
private Paint paint;
private Canvas canvas;
private ImageButton jj;
private Bitmap up,down,left,right,centre;
public MySurfaceView(Context context) {
super(context);
sfh=this.getHolder();
sfh.addCallback(this);
paint=new Paint();
up=BitmapFactory.decodeResource(this.getResources(), R.drawable.up_no);
down=BitmapFactory.decodeResource(this.getResources(), R.drawable.down_no);
left=BitmapFactory.decodeResource(this.getResources(), R.drawable.left_no);
right=BitmapFactory.decodeResource(this.getResources(), R.drawable.right_no);
centre=BitmapFactory.decodeResource(this.getResources(), R.drawable.centre);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
myDraw();
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
}
private void myDraw(){
try{
canvas=sfh.lockCanvas();
if(canvas!=null){
canvas.drawRGB(255,255,255);
canvas.save(); //up键
canvas.scale(1.5f, 1.5f,50+up.getWidth(),50+up.getHeight());
canvas.drawBitmap(up,200,950,paint);
canvas.restore();
canvas.save(); //down键
canvas.scale(1.5f, 1.5f,50+down.getWidth(),50+down.getHeight());
canvas.drawBitmap(down,200,1150,paint);
canvas.restore();
canvas.save();//left键
canvas.scale(1.5f, 1.5f,50+left.getWidth(),50+left.getHeight());
canvas.drawBitmap(left,100,1060,paint);
canvas.restore();
canvas.save();//right键
canvas.scale(1.5f, 1.5f,50+right.getWidth(),50+right.getHeight());
canvas.drawBitmap(right,300,1060,paint);
canvas.restore();
canvas.save();
canvas.scale(1f, 1f,centre.getWidth(),centre.getHeight());
canvas.drawBitmap(centre,250,1525,paint);
canvas.restore();
}
}catch(Exception e){
}finally{
if(canvas!=null)
sfh.unlockCanvasAndPost(canvas);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// float value = 0;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
y_down = event.getY();
break;
case MotionEvent.ACTION_MOVE:
y_up = event.getY();
value = y_down - y_up;
this.invalidate();
break;
case MotionEvent.ACTION_UP:
initColorView();
break;
}
return true;
}