跟隨手指的小球,跟隨手指小球

來源:互聯網
上載者:User

跟隨手指的小球,跟隨手指小球

    

 

Crize_demo\draw_demo\src\main\java\com\ly\draw_demo\DrawView.java

 1 import android.content.Context; 2 import android.graphics.Canvas; 3 import android.graphics.Color; 4 import android.graphics.Paint; 5 import android.view.View; 6  7 public class DrawView extends View { 8  9     public float currentX = 40;10     public float currentY = 50;11 12     public DrawView(Context context) {13         super(context);14     }15 16     @Override17     protected void onDraw(Canvas canvas) {18         super.onDraw(canvas);19         //建立畫筆20         Paint paint = new Paint();21         //設定畫筆的顏色22         paint.setColor(Color.RED);23         //繪製一個小圓(作為小球)24         canvas.drawCircle(currentX,currentY,15,paint);25     }26 }

 

Crize_demo\draw_demo\src\main\res\layout\activity_main.xml

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"2     xmlns:tools="http://schemas.android.com/tools"3     android:layout_width="match_parent"4     android:layout_height="match_parent"5     android:orientation="horizontal"6     android:id="@+id/root"7     tools:context=".MainActivity">8 9 </LinearLayout>

 

Crize_demo\draw_demo\src\main\java\com\ly\draw_demo\MainActivity.java

 1 import android.app.Activity; 2 import android.os.Bundle; 3 import android.view.MotionEvent; 4 import android.view.View; 5 import android.widget.LinearLayout; 6  7 public class MainActivity extends Activity { 8      9     @Override10     protected void onCreate(Bundle savedInstanceState) {11         super.onCreate(savedInstanceState);12         setContentView(R.layout.activity_main);13         //擷取布局檔案中的LinearLayout容器14         LinearLayout root = (LinearLayout) findViewById(R.id.root);15         //建立DrawView組件16         final DrawView drawView = new DrawView(this);17         //設定自訂架構的寬度和高度18         drawView.setMinimumWidth(300);19         drawView.setMinimumHeight(500);20         //為drawView綁定Touch事件(觸控螢幕幕的監聽事件)21         drawView.setOnTouchListener(new View.OnTouchListener() {22             @Override23             public boolean onTouch(View v, MotionEvent event) {24                 //修改drawView組件的currentX,currentY兩個屬性25                 drawView.currentX = event.getX();26                 drawView.currentY = event.getY();27                 //通知drawView組件重繪28                 drawView.invalidate();29                 //返回true表明處理方法已經處理該事件30                 return true;31             }32         });33         root.addView(drawView);34     }35 36 37 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.