android 拖动图片拖动浮动按钮.docx_第1页
android 拖动图片拖动浮动按钮.docx_第2页
android 拖动图片拖动浮动按钮.docx_第3页
android 拖动图片拖动浮动按钮.docx_第4页
android 拖动图片拖动浮动按钮.docx_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

android 拖动图片/拖动浮动按钮Java代码 import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.os.Bundle; import android.view.MotionEvent ; import android.widget.AbsoluteLayout; import android.widget.Button; public class Drag_And_Drop extends Activity /* Called when the activity is first created. */ Override public void onCreate(Bundle icicle) super.onCreate(icicle); MyView tx = new MyView(this); tx.setText(Drag Me); AbsoluteLayout l = new AbsoluteLayout(this); AbsoluteLayout.LayoutParams p = new AbsoluteLayout.LayoutParams( AbsoluteLayout.LayoutParams.WRAP_CONTENT, AbsoluteLayout.LayoutParams.WRAP_CONTENT,10,10); l.addView(tx,p); setContentView(l); class MyView extends Button public MyView(Context c) super(c); Override public boolean onMotionEvent(MotionEvent event) int action = event.getAction(); int mCurX = (int)event.getX(); int mCurY = (int)event.getY(); if ( action = MotionEvent.ACTION_MOVE ) /this.setText(x: + mCurX + ,y: + mCurY ); AbsoluteLayout.LayoutParams p = new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT, AbsoluteLayout.LayoutParams.WRAP_CONTENT,this.mLeft + mCurX,this.mTop + mCurY); this.setLayoutParams (p); if ( action = MotionEvent.ACTION_UP ) /this.setText(not moving); return true; Override public void draw(Canvas canvas) / TODO Auto-generated method stub super.draw(canvas); 拖拽图片效果 方法一: Java代码 import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; public class DragSample01 extends Activity ImageView img; Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.drag_sample01); img = (ImageView)findViewById(R.id.img_view); img.setOnTouchListener(new OnTouchListener() private int mx, my; public boolean onTouch(View v, MotionEvent event) switch(event.getAction() case MotionEvent.ACTION_MOVE: mx = (int)(event.getRawX(); my = (int)(event.getRawY() - 50); v.layout(mx - img.getWidth()/2, my - img.getHeight()/2, mx + img.getWidth()/2, my + img.getHeight()/2); break; return true; ); 方法二: Java代码 import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; public class DragSample01 extends Activity ImageView img; Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.drag_sample01); img = (ImageView)findViewById(R.id.img_view); img.setOnTouchListener(new OnTouchListener() private float x, y; private int mx, my; public boolean onTouch(View v, MotionEvent event) switch(event.getAction() case MotionEvent.ACTION_DOWN: x = event.getX(); y = event.getY(); case MotionEvent.ACTION_MOVE: mx = (int)(event.getRawX() - x); my = (int)(event.getRawY() - 50 - y); v.layout(mx, my, mx + v.getWidth(), my + v.getHeight(); break; return true; ); 拖动按钮到处跑 1. 布局文件 Xml代码 2. 代码 Java代码 import android.app.Activity; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.Button; public class DraftTest extends Activity /* Called when the activity is first created. */ Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button btn = (Button) findViewById(R.id.btn); btn.setOnTouchListener(new OnTouchListener() int temp = new int 0, 0 ; public boolean onTouch(View v, MotionEvent event) int eventaction = event.getAction(); int x = (int) event.getRawX(); int y = (int) event.getRawY(); switch (eventaction) case MotionEvent.ACTION_DOWN: / touch down so check if the temp0 = (int) event.getX(); temp1 = y - v.getTop(); break; case MotionEvent.ACTION_MOVE: / touch drag with the ball v.layout(x - temp0, y - temp1, x + v.getWidth() - temp0, y - temp1 + v.getHeight(); / v.postInvalidate(); break; case MotionEvent.ACTION_UP: break; return false; ); 另一种: Java代码 import android.app.Activity; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.Button; public class DraftTest extends Activity /* Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); DisplayMetrics dm = getResources().getDisplayMetrics(); final int screenWidth = dm.widthPixels; final int screenHeight = dm.heightPixels - 50; final Button b = (Button) findViewById(R.id.btn); b.setOnTouchListener(new OnTouchListener() int lastX, lastY; public boolean onTouch(View v, MotionEvent event) / TODO Auto-generated method stub switch (event.getAction() case MotionEvent.ACTION_DOWN: lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break; case MotionEvent.ACTION_MOVE: int dx = (int) event.getRawX() - lastX; int dy = (int) event.getRawY() - lastY; int left = v.getLeft() + dx; int top = v.getTop() + dy; int right = v.getRight() + dx; int bottom = v.getBottom() + dy; if (left screenWidth) right = screenWidth; left = right - v.getWidth(); if (top screenHeight) bottom = screenHeight; top = bottom - v.getHeight(); v.layout(left, top, right, bottom); lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break; case MotionEvent.ACTION_UP: break; return false; ); 再一个,浮动按钮的实现。 主要功能: 点击按钮可以进行拖动; 当点击按钮时按钮会出现于所有按钮的最上方; Java代码 import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.text.style.AbsoluteSizeSpan; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.widget.AbsoluteLayout; import android.widget.Button; public class HelloWorld2 extends Activity /* Called when the activity is first created. */ AbsoluteLayout mLayoutGroup = null; Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); /setContentView(R.layout.main); mLayoutGroup = new AbsoluteLayout(this); AbsoluteLayout.LayoutParams layoutParams = new AbsoluteLayout.LayoutParams (320, 480, 0, 0); setContentView(mLayoutGroup, layoutParams); Button button= new Button(this); button.setText(testButton); layoutParams = new AbsoluteLayout.LayoutParams(120, 60, 20, 20); mLayoutGroup.addView(button, layoutParams); button.setOnClickListener(new OnClickListener() public void onClick(View arg0) / TODO Auto-generated method stub /alert(); ); button.setOnTouchListener(touchListener); final Button btButton = new Button(this); btButton.setText(测试按钮移动); layoutParams = new AbsoluteLayout.LayoutParams(120, 60, 20, 160); mLayoutGroup.addView(btButton, layoutParams); btButton.setOnTouchListener(touchListener); OnTouchListener touchListener = new OnTouchListener() int temp = new int0, 0; public boolean onTouch(View arg0, MotionEvent arg1) / TODO Auto-generated method stub int eventAction = ar

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论