android怎麼實現 映像隨著手指的移動而移動

來源:互聯網
上載者:User

標籤:android   http   color   io   ar   使用   java   sp   檔案   

============問題描述============


android怎麼實現   映像隨著手指的移動而移動  當點擊一個地方的時候圖片能夠移動到那個地方   希望給出具體的範例程式碼

============解決方案1============


activity內容:
public class CustomView extends Activity{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);}}


DrawView組件:

public class DrawView extends View{public float currentX = 40;public float currentY = 50;/** * @param context */public DrawView(Context context , AttributeSet set){super(context , set);}@Overridepublic void onDraw (Canvas canvas){super.onDraw(canvas);//建立畫筆Paint p = new Paint();//設定畫筆的顏色p.setColor(Color.RED);//繪製一個小圓(作為小球)canvas.drawCircle(currentX , currentY , 15 , p);}@Overridepublic boolean onTouchEvent(MotionEvent event){//當前組件的currentX、currentY兩個屬性this.currentX = event.getX();this.currentY = event.getY();//通知改組件重繪this.invalidate();//返回true表明處理方法已經處理該事件return true;}}


xml布局檔案:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 使用自訂群組件 --><org.crazyit.event.DrawViewandroid:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"/></LinearLayout>



代碼引用自《瘋狂安卓講義》李剛

android怎麼實現 映像隨著手指的移動而移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.