zoukankan      html  css  js  c++  java
  • 拖动球

              在这里我想说的是在规定的区域用手拖动一个自定义的小球,很简单,首先定义一个画小球的类,在里面重写ondraw方法,然后在main文件中实例化刚才的类,之后呢给他一个触摸事件就会随你的手指移动了……

             1.先看我们画小球的那个类吧!代码如下:

                  public class DrawView extends View

                  {

                      public float currentX=40;

                      public float currentY=50;

                      public DrawView (Context context){

                                     super(context);

                           }            

                       protected void onDraw(Canvas canvas){

                               super.onDraw(canvas);

                               Paint p=new Paint();

                               p.setColor(Color.BULE);

                               canvas.drawCircle(currentX,currentY,15,p);

                          }

                   }

              2.定义好圆后我们来写main里面的,还是看代码吧!

                   public class MainActivity extends Activity{

                      protected void onCreate(){

                      super.onCreate(savedInstanceState);

                      LinearLayout root=(LinearLayout)findViewById(R.id.LinearLayout1);

                      final DrawView draw=new DrawView(this);

                      draw.setMinimumHeight(400);

                      draw.setMinimumWidth(500);

                      draw.setOnTouchListener(new OnTouchListener(){

                                   public boolean onTouch(View arg0,MotionEvent arg1){

                                  draw.currentX.=arg1.getX();

                                   draw.currentY=arg1.getY();

                                   //通知draw组件重新绘制

                                    draw.invalidate();

                                    retrun true;

                                   }

                               });

                              root.addView(draw); 

                           }

                     }

      

  • 相关阅读:
    24.最优布线问题(kruskal算法)
    24.最优布线问题(kruskal算法)
    Algs4-1.4.11为StaticSETofInts添加一个实列方法howMany()
    Algs4-1.4.9预测程序运行时间
    Algs4-1.4.10二分查找找出元素所在的最小索引
    Algs4-1.4.7统计算术运算与比较次数
    Algs4-1.4.8计算输入文件中相等的整数对的数量
    Algs4-1.4.6给出以下代码段的运行时间的增长数量级
    Algs4- 1.4.4参照表1.4.4为TwoSum建立一和类似的表格
    Algs4-1.4.2修改ThreeSum防止两个int值相加可能溢出
  • 原文地址:https://www.cnblogs.com/lk119/p/3236440.html
Copyright © 2011-2022 走看看