欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

基于Android实现随手指移动的ImageView

程序员文章站 2023-12-22 13:58:28
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>

实现代码

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实现随手指移动的imageview的相关知识,希望本文分享对大家有所帮助。

上一篇:

下一篇: