以下利用一个app来说明怎样利用自己定义View的重绘实现拖动移动。获取组件的尺寸。
例如以下图,触摸拖动,或者轻轻点击屏幕都能移动图片。假设碰到文字,则会弹出提示。
这里是利用自己定义View的重绘来实现的。就是点击屏幕一次,这个自己定义View就会重绘一次。尽管这个自己定义View里面就仅仅有一个图片。
1、首先在resvaluesstrings.xml中定义各个字体文件,改动之后例如以下:
<?2、之后改动MainActivity.java的布局文件reslayoutactivity_main.xml。在帧布局中里面就放置一个TextView,这个TextView偏离头部100sp,给个id。在MainActivity.java会通过Java代码。获取其在设备的绝对尺寸与位置。不同设备对于sp的解析是不同的。xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">拖动移动</string> <string name="action_settings">Settings</string> <string name="textView1">触摸白色屏幕任何位置能移动图片</string> </resources>
而自己定义View,ImageLayout。一会儿通过代码生成。无须在这里布置。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/frameLayout1" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="100sp" android:text="@string/textView1" android:textSize="24sp" /> </FrameLayout>3、如同《【Android】自己定义View、画布Canvas与画笔Paint》(点击打开链接)一样,自己定义一个自己定义View。ImageLayout。在src新建ImageLayout.java之后,改动其代码例如以下,初始化画笔,与图像文件,图像文件资源指定为自带的app图标。定义一个可被其他类、方法操作的x,y。在onDraw方法中。依据这个x,y绘制这个图像。
最后同一时候要回收上次绘制出来的图像。
package com.touch_move; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.view.View; public class ImageLayout extends View { public float x; public float y; private Paint paint; public Bitmap bitmap; public ImageLayout(Context context) { super(context); paint = new Paint(); bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawBitmap(bitmap, x, y, paint); if (bitmap.isRecycled()) { bitmap.recycle(); } } }4、之后是对MainActivity.java的方法改动,这种方法分为三部分,第一部分是注冊各个组件与加入事件,没什么好说的。第二部分是自己定义布局ImageLayout的触摸监听器的实现。最后是获取各个组件的尺寸,不能够在onCreate方法中获取组件尺寸。这是安卓的机制所决定了。那时候组件还没有全然生成。获取到尺寸将会是0。
注意获取组件的时候,获取的是自己定义布局中的图片ImageLayout.bitmap的尺寸,而不是自己定义布局ImageLayout。自己定义布局是覆盖整个帧布局。因此你在app中,点击随意白色位置,其触摸事件都生效。
在实现触摸事件中,因为默认是把触摸点为左上角,绘制头像的。因此要作图片尺寸的编译,使图片的中央生成在光标的点击处。
触摸事件的返回值记得是false,否则这个触摸事件会返回两次。
package com.touch_move; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.FrameLayout; import android.widget.TextView; import android.widget.Toast; import android.app.Activity; public class MainActivity extends Activity implements OnTouchListener { private FrameLayout frameLayout1; private TextView textView1; private ImageLayout imageLayout; private int textView1Left; private int textView1Top; private int textView1Width; private int textView1Height; private int bitmapWidth; private int bitmapHeight; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); frameLayout1 = (FrameLayout) findViewById(R.id.frameLayout1); textView1 = (TextView) findViewById(R.id.textView1); imageLayout = new ImageLayout(this); imageLayout.setOnTouchListener(this); frameLayout1.addView(imageLayout); } @Override public boolean onTouch(View view, MotionEvent motionEvent) { imageLayout.x = motionEvent.getX() - bitmapWidth / 2; imageLayout.y = motionEvent.getY() - bitmapHeight / 2; imageLayout.invalidate(); if ((textView1Left < motionEvent.getX()) && (motionEvent.getX() < (textView1Left + textView1Width)) && (textView1Top < motionEvent.getY()) && (motionEvent.getY() < (textView1Top + textView1Height)) ) { Toast.makeText(this, "图片与文字撞在一起了……", Toast.LENGTH_SHORT).show(); } return false; } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); bitmapWidth = imageLayout.bitmap.getWidth(); bitmapHeight = imageLayout.bitmap.getHeight(); textView1Left = textView1.getLeft(); textView1Top = textView1.getTop(); textView1Width = textView1.getWidth(); textView1Height = textView1.getHeight(); } }使用imageLayout.invalidate();方法可以又一次运行自己定义布局类的ImageLayout.java的全部方法,也就是把这个自己定义布局重绘了一次。
从而实现这个组件的移动。