
我想制作一个拖放按钮.将它拖动到您想要的位置并保持不变.下面的代码只缩放按钮,不会改变它的位置.
package com.dynamic;import androID.app.Activity;import androID.os.Bundle;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup.LayoutParams;import androID.Widget.button;import androID.Widget.FrameLayout;import androID.Widget.ImageVIEw;import androID.Widget.TextVIEw;import com.dynamic.R;import com.dynamic.R.layout;public class dy extends Activity { int status; private FrameLayout layout; ImageVIEw image; button b; LayoutParams params; public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.frm); final button tv=(button)findVIEwByID(R.ID.txt_birth); tv.setDrawingCacheEnabled(true); layout = (FrameLayout) findVIEwByID(R.ID.frm); params = new LayoutParams(100,100); params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); tv.setontouchListener(new VIEw.OntouchListener(){ @OverrIDe public boolean ontouch(VIEw v, MotionEvent me) { // Todo auto-generated method stub if (me.getAction() == MotionEvent.ACTION_DOWN) { System.out.println("up"); status = 0; } if (me.getAction() == MotionEvent.ACTION_UP) { status = 1; //Log.i("Drag", "Stopped Dragging"); } else if (me.getAction() == MotionEvent.ACTION_MOVE) { if (status == 0) { System.out.println("Dragging"); tv.setpadding((int) me.getRawX(), (int) me.getRawY(), 0, 0); // b.setpadding(0,50,0,0); tv.invalIDate(); } } return false; } });}}解决方法:
我正在研究类似的事情.这是我正在使用的OntouchListener:
myOntouchListener = new OntouchListener() { public boolean ontouch(VIEw v, MotionEvent me){ if (me.getAction() == MotionEvent.ACTION_DOWN){ oldXvalue = me.getX(); oldYvalue = me.getY(); Log.i(mytag, "Action Down " + oldXvalue + "," + oldYvalue); }else if (me.getAction() == MotionEvent.ACTION_MOVE ){ LayoutParams params = new LayoutParams(v.getWIDth(), v.getHeight(),(int)(me.getRawX() - (v.getWIDth() / 2)), (int)(me.getRawY() - (v.getHeight()))); v.setLayoutParams(params); } return true; } };v是您想要移动的视图,在您的情况下,您将用您的按钮替换v.另请注意,为了使其工作,我必须使用absoluteLayout作为我的xml文件中的父视图.我知道它已被弃用但是使用它然后使用relativeLayout并尝试动态设置边距以移动视图似乎更合乎逻辑.我用于新x和y位置的公式试图使其在移动时以手指为中心.但它并不完美,取决于视图的大小,它仍然会偏离你的手指中心.
总结以上是内存溢出为你收集整理的如何在Android中制作拖放按钮全部内容,希望文章能够帮你解决如何在Android中制作拖放按钮所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)