Android編程實現表徵圖拖動效果的方法_Android

來源:互聯網
上載者:User

本文執行個體講述了Android編程實現表徵圖拖動效果的方法。分享給大家供大家參考,具體如下:

最近最佳化表徵圖拖動時的速率,稍微有一點點效果,直接把代碼貼出來,有興趣一起討論的朋友可以給我留言。

代碼如下:

DragView.java

package com.android.dragtest;import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.widget.FrameLayout;public class DragView extends FrameLayout { private static final String TAG = "DragView"; private float X; private float Y; private View mDragView; public DragView(Context context) {  this(context, null); } public DragView(Context context, AttributeSet attrs) {  this(context, attrs, 0); } public DragView(Context context, AttributeSet attrs, int defStyle) {  super(context, attrs, defStyle);  mDragView = new View(context);  mDragView.setLayoutParams(new LayoutParams(60, 60));  mDragView.setBackgroundDrawable(getResources().getDrawable(R.drawable.gamecenter));  mDragView.setVisibility(View.INVISIBLE);  addView(mDragView); } public boolean onInterceptTouchEvent(MotionEvent ev) {  final int action = ev.getAction();  switch (action) {  case MotionEvent.ACTION_DOWN:   Log.d(TAG, "===============>onInterceptTouchEvent ACTION_DOWN");   break;  case MotionEvent.ACTION_MOVE:   Log.d(TAG, "===============>onInterceptTouchEvent ACTION_MOVE");   break;  case MotionEvent.ACTION_UP:   Log.d(TAG, "===============>onInterceptTouchEvent ACTION_UP");   break;  }  return true; } public boolean onTouchEvent(MotionEvent ev) {  final int action = ev.getAction();  X = ev.getX();  Y = ev.getY();  switch (action) {  case MotionEvent.ACTION_DOWN:   Log.d(TAG, "onTouchEvent ACTION_DOWN");   mDragView.layout((int)X - 30, (int)Y - 30, (int)X + 30, (int)Y + 30);   mDragView.setVisibility(View.VISIBLE);   break;  case MotionEvent.ACTION_MOVE:   Log.d(TAG, "onTouchEvent ACTION_MOVE x:" + X + " Y:" + Y);   mDragView.layout((int)X - 30, (int)Y - 30, (int)X + 30, (int)Y + 30);   break;  case MotionEvent.ACTION_UP:   Log.d(TAG, "onTouchEvent ACTION_UP");   mDragView.setVisibility(View.INVISIBLE);   break;  }  return true; }}

DragTestActivity.java

package com.android.dragtest;import android.app.Activity;import android.os.Bundle;public class DragTestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main); }}

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.android.dragtest.DragView  android:layout_width="match_parent"  android:layout_height="match_parent"> </com.android.dragtest.DragView></LinearLayout>

希望本文所述對大家Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.