package com.example.sft; import java.util.ArrayList; import java.util.List; import java.util.Random; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.RadialGradient; import android.graphics.Shader; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; public class TestSurfaceView extends SurfaceView implements SurfaceHolder.Callback{ private Paint cp; private List<KCircle> KCList; private Random r; public TestSurfaceView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub getHolder().addCallback(this); cp=new Paint(); cp.setColor(Color.BLACK); KCList=new ArrayList<KCircle>(); r=new Random(); } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } private int getRandomColor(){ return r.nextInt()%0xff000000; } @Override public void surfaceCreated(SurfaceHolder arg0) { // TODO Auto-generated method stub } /** * */ public void run(){ new MyThread().start(); } @Override public void surfaceDestroyed(SurfaceHolder arg0) { // TODO Auto-generated method stub } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub if (event.getAction() != MotionEvent.ACTION_DOWN && event.getAction() != MotionEvent.ACTION_MOVE) { return false; } KCList.add(new KCircle(getRandomColor(),(int)event.getX(),(int)event.getY())); return true; } class MyThread extends Thread{ @Override public void run() { // TODO Auto-generated method stub while(true){ Canvas c=getHolder().lockCanvas(); c.drawPaint(cp); for(int i=0;i<KCList.size();i++){ KCList.get(i).moveAndDraw(c); if(KCList.get(i).y>getHeight()){ KCList.remove(i); } } try { sleep(15); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } getHolder().unlockCanvasAndPost(c); } } } }
package com.example.sft; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RadialGradient; import android.graphics.Shader; public class KCircle { private int color; public int x; public int y; private Paint p; private int radius; int v; int a; public KCircle(int color){ this.color=color; x=0; y=0; v=0; a=2; radius=25; p=new Paint(); p.setColor(color); RadialGradient rg=new RadialGradient(x+10, y-10, 25,color+(0xffffffff-color)/2, color, Shader.TileMode.CLAMP); p.setShader(rg); p.setAntiAlias(true); } public KCircle(int colcr,int x,int y){ this(colcr); this.x=x; this.y=y; } public void moveAndDraw(Canvas c){ y+=v; v+=a; draw(c); } public void moveTo(int x,int y){ this.x=x; this.y=y; } public void draw(Canvas c){ RadialGradient rg=new RadialGradient(x+10, y-10, 25,0xffdddddd, color, Shader.TileMode.CLAMP); p.setShader(rg); c.drawCircle(x, y, radius, p); } }
把TestSurfaceView放入Activity显示出来 然后调用run方法 手指点击的地方就会有小球落下