亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android實現(xiàn)拖動小球跟隨手指移動效果

 更新時間:2020年08月24日 08:54:44   作者:G_T_K  
這篇文章主要為大家詳細介紹了Android實現(xiàn)拖動小球跟隨手指移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Android拖動小球跟隨手指移動Demo,供大家參考,具體內(nèi)容如下

1、使用的知識點有自定義View,利用Canvas畫球;
2、使用觸摸時間來操作;

效果圖:

代碼如下:

1、自定義view;

public class DrawView extends View {
 public float currentX = 50;
 public float currentY = 50;

 public DrawView(Context context) {
  super(context);
 }
 public void onDraw(Canvas canvas){
  super.onDraw(canvas);
  Paint paint = new Paint();
  paint.setColor(Color.RED);
  canvas.drawCircle(currentX,currentY,10,paint);
 }
}

2、顯示;

public class MainActivity extends Activity {

 public LinearLayout linearLayout;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  linearLayout = (LinearLayout) findViewById(R.id.root);

  final DrawView drawView = new DrawView(this);
  drawView.setOnTouchListener(new View.OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    drawView.currentX = event.getX();
    drawView.currentY = event.getY();
    //通過draw組件重繪
    drawView.invalidate();

    return true;
   }
  });
  linearLayout.addView(drawView);
 }
}

3、布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 android:id="@+id/root"
 tools:context="com.syt.androidtest.androidtest1.MainActivity">


</LinearLayout>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論