【转】Android 拖动Layout上的图片-非Canvas

来源:http://androidrox.wordpress.com/2011/05/13/android-sample-app-drag-and-drop-image-using-touch/

摘要:这个是一个layout上定义了一张图片,以及对其拖动的处理。

             源码是个人根据网上的方法做的。

             老样子,工程文件打成包传上来的,请右击保存成TouchImage.zip ,解压缩后即可用Eclipse导入工程。

              过几天把自定义控件的方式给传上来


------------------------------------------------------------------------------------------------------------------------------

下面附上网页的拷贝

Android sample App : Drag And Drop Image using Touch

Posted: May 13, 2011 by AndroidRox in Uncategorized
0

Hello,

Below code is for drag and drop image in any location on android screen phone….

these all done using TouchListner…

check it out.



 TouchActivity.java

package com.example.Touch;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.AbsoluteLayout.LayoutParams;

public class TouchActivity extends Activity{
/** Called when the activity is first created. */
ImageView img=null;
AbsoluteLayout aLayout;
int status=0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
aLayout= (AbsoluteLayout)findViewById(R.id.absLayout);

img=(ImageView)findViewById(R.id.imageView);

//sa.setFillAfter(true);

img.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {

status=1;
Log.i(“ImageStatus”,”"+status);
//img.setBackgroundColor(Color.WHITE);

return false;
}
});
aLayout.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.i(“touch”,”"+event);

if(status==1) // any event from down and move
{
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,(int)event.getX()-img.getWidth()/2,(int)event.getY()-img.getHeight()/2);
img.setLayoutParams(lp);

}
if(event.getAction()==MotionEvent.ACTION_UP){
status=0;

img.setBackgroundColor(Color.TRANSPARENT);

}
return true;
}
});
}

}

——————————————————————————————————————————————-

Main.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”
android:id=”@+id/LLayout”
>
<AbsoluteLayout
android:id=”@+id/absLayout”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<ImageView android:layout_height=”wrap_content” android:id=”@+id/imageView” android:layout_width=”wrap_content” android:src=”@drawable/icon”></ImageView>
</AbsoluteLayout>
</LinearLayout>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值