基于Android实现随手指移动的ImageView
ImageView用来显示任意图像图片,可以自己定义显示尺寸,显示颜色等等.
运行效果是这样的(文字说明):
首次进入程序,手指点击屏幕上的任意位置,图片会随之移动。
布局文件
<?xmlversion="1.0"encoding="utf-8"?> <FrameLayoutxmlns: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>
实现代码
publicclassMoveImageViewextendsImageView{
publicMoveImageView(Contextcontext){
super(context);
}
publicMoveImageView(Contextcontext,AttributeSetattrs){
super(context,attrs,0);
}
publicMoveImageView(Contextcontext,AttributeSetattrs,intdefStyle){
super(context,attrs,defStyle);
}
publicvoidsetLocation(intx,inty){
this.setFrame(x,y-this.getHeight(),x+this.getWidth(),y);
}
//移动
publicbooleanautoMouse(MotionEventevent){
booleanrb=false;
switch(event.getAction()){
caseMotionEvent.ACTION_MOVE:
this.setLocation((int)event.getX(),(int)event.getY());
rb=true;
break;
}
returnrb;
}
}
publicclassTestImageViewMoveextendsActivity{
privateMoveImageViewmoveImageView;
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
moveImageView=(MoveImageView)this.findViewById(R.id.ImageView01);
}
@Override
publicbooleanonTouchEvent(MotionEventevent){
moveImageView.autoMouse(event);
returnfalse;
}
}
以上内容给大家介绍了基于Android实现随手指移动的ImageView的相关知识,希望本文分享对大家有所帮助。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下