基于Android實現(xiàn)隨手指移動的ImageView
更新時間:2016年01月11日 10:56:02 作者:IAlexanderI
這篇文章主要介紹了基于Android實現(xiàn)隨手指移動的ImageView的相關資料,需要的朋友可以參考下
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.MoveImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon" > </com.sgw.move.MoveImageView> </FrameLayout>
實現(xiàn)代碼
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);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
moveImageView.autoMouse(event);
return false;
}
}
以上內容給大家介紹了基于Android實現(xiàn)隨手指移動的ImageView的相關知識,希望本文分享對大家有所幫助。
相關文章
Android如何讓APP無法在指定的系統(tǒng)版本上運行(實現(xiàn)方法)
這篇文章主要介紹了Android如何讓APP無法在指定的系統(tǒng)版本上運行(實現(xiàn)方法),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
Android使用NestedScrollView?內嵌RecycleView滑動沖突問題解決
這篇文章主要介紹了Android使用NestedScrollView?內嵌RecycleView滑動沖突問題解決,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-06-06

