欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)實(shí)現(xiàn)跟隨手指的小球效果示例

 更新時(shí)間:2019年04月17日 14:16:17   作者:水中魚之1999  
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)跟隨手指的小球效果,涉及Android圖形繪制、事件響應(yīng)、界面布局等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android開發(fā)實(shí)現(xiàn)跟隨手指的小球效果。分享給大家供大家參考,具體如下:

配置DrawView類用于繪制小球

public class DrawView extends View {
  public float currentX = 40;
  public float currentY = 50;
  //定義并創(chuàng)建畫筆
  Paint p = new Paint();
  public DrawView(Context context)
  {
    super(context);
  }
  public DrawView(Context context , AttributeSet set)
  {
    super(context,set);
  }
  @Override
  public void onDrawForeground(Canvas canvas) {
    super.onDrawForeground(canvas);
    //設(shè)置畫筆顏色
    p.setColor(Color.RED);
    //繪制一個(gè)小球
    canvas.drawCircle(currentX , currentY , 30 , p);
  }
  //為組建的觸碰實(shí)踐重寫處理方法
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    //修改currentX,currentY的兩個(gè)屬性
    currentX = event.getX();
    currentY = event.getY();
    //通知當(dāng)前組建重繪自己
    invalidate();
    //放回true表明該處理方法已經(jīng)處理該事件
    return true;
  }
}

MainActivity

public class MainActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //獲取Linearlayout布局容器
    LinearLayout root = (LinearLayout) findViewById(R.id.root);
    //創(chuàng)建DrawView組件
    final DrawView draw = new DrawView(this);
    //設(shè)定自定義組件的最小寬度、高度
    draw.setMinimumWidth(300);
    draw.setMinimumHeight(500);
    root.addView(draw);
  }
}

xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/root"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <com.example.a30797.myapplication.DrawView
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

**注:**由上面布局,已經(jīng)添加了自定義組件,因此Activity代碼可簡化為:

public class MainActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }
}

示例:

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》及《Android資源操作技巧匯總

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論