zoukankan      html  css  js  c++  java
  • 【备忘】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方法   手指点击的地方就会有小球落下

  • 相关阅读:
    并发编程与高并发学习笔记六
    并发编程与高并发学习笔记五
    并发编程与高并发学习笔记四
    浅谈数据挖掘与机器学习
    IntelliJ Idea常用的快捷键
    单链表的插入,查找,删除
    二叉树的序列化和反序列化及完全二叉树和满二叉树的区别
    二叉树的按层遍历并实现换行
    冒泡排序,选择排序,插入排序,快速排序的比较及优化
    axSpA患者新发炎症更容易发生在既往发生过炎症的区域
  • 原文地址:https://www.cnblogs.com/kagami/p/3009995.html
Copyright © 2011-2022 走看看