基於Android實現隨手指移動的ImageView_Android

來源:互聯網
上載者:User

ImageView用來顯示任意映像圖片,可以自己定義顯示尺寸,顯示顏色等等.

運行效果是這樣的(文字說明):

首次進入程式,手指點擊螢幕上的任意位置,圖片會隨之移動。

布局檔案

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/FrameLayout01"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="#f0f0f0" ><com.sgw.move.MoveImageViewandroid:id="@+id/ImageView01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/icon" ></com.sgw.move.MoveImageView></FrameLayout>

實現代碼

public class MoveImageView extends ImageView {public MoveImageView(Context context) {super(context);}public MoveImageView(Context context, AttributeSet attrs) {super(context, attrs, 0);}public MoveImageView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public void setLocation(int x, int y) {this.setFrame(x, y - this.getHeight(), x + this.getWidth(), y);}// 移動public boolean autoMouse(MotionEvent event) {boolean rb = false;switch (event.getAction()) {case MotionEvent.ACTION_MOVE:this.setLocation((int) event.getX(), (int) event.getY());rb = true;break;}return rb;}}public class TestImageViewMove extends Activity {private MoveImageView moveImageView;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);moveImageView = (MoveImageView) this.findViewById(R.id.ImageView01);}@Overridepublic boolean onTouchEvent(MotionEvent event) {moveImageView.autoMouse(event);return false;}}

以上內容給大家介紹了基於Android實現隨手指移動的ImageView的相關知識,希望本文分享對大家有所協助。

聯繫我們

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