• 【备忘】Android模拟小球自由落体(SurfaceView)


    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方法   手指点击的地方就会有小球落下

  • 相关阅读:
    js实现的hashMap
    vi编辑器常用命令汇总以及linux系统操作的命令(自己需要用到什么,经过测试正确的,会持续向上添加)
    使用conda,提示-bash: conda: command not found
    tmux常用命令
    OSError: [WinError 126] JVM DLL not found: C:Program FilesJavajdk1.8.0_131jreinserverjvm.dll
    python建立连接,获取动态地址,有缺陷
    python代码中,添加子进程的运行
    python通过建立长链接,获取动态ip,这种方式可以长时间保持连接
    ImportError: cannot import name PDFDocument
    http请求参数之Query String Parameters、Form Data、Request Payload
  • 原文地址:https://www.cnblogs.com/kagami/p/3009995.html
Copyright © 2020-2023  润新知