Android自定义View拖拽效果的设计

46 篇文章 0 订阅

Android自定义View的拖拽效果的设计

这里设计两个程序自定义View效果:
第一个程序效果:
x1

点击图像,图像随手指的一定而一定。移动只要的靠的就是View中的layout(int left,int top,int right ,int button)方法。

第二个程序效果:
x2

拖动两个图像到上下接壤的位置,拖动上面一个可以一起拖动这两个图像,拖动下面一个图像,则只会拖动下面一个图像。
这里偏移距离用的还是layout方法,但是需要试用到回掉接口,在其中一个View移动时判断是否一起移动,再回调数据。
分析:
第一个程序只需要重写自定义View的类,所有的处理都是在自定义View中编写的,只要把这个自定义类放到布局文件中,点击它就会有拖拽的效果。
第二个程序处理要写两个自定义View,并且要定义回调接口,在拖拽其中一个View时,第二个也随之被拖动,这里就要在回调中返回移动的距离,从而确定另一个View要移动的距离。

代码:

第一个程序:

(1)自定义View的类的设计

package com.example.myview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * 自定义的View
 */

public class MyImageView extends ImageView {

    private int lastX;
    private int lastY;

    public MyImageView(Context context) {
        super(context);

    }

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }



    public boolean onTouchEvent(MotionEvent event) {

        //获取到手指处的横坐标和纵坐标
        int x = (int) event.getX();
        int y = (int) event.getY();

        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN://手指按下时
                lastX = x;
                lastY = y;
                break;
            case MotionEvent.ACTION_MOVE://手指移动时
                //计算移动的距离
                int offX = x - lastX;
                int offY = y - lastY;
                //调用layout方法来重新放置它的位置
                Log.e("TAG","距离左边="+getLeft()+offX+",距离顶部="+getTop()+offY+",距离右边="+ getRight()+offX+",距离底部="+ getBottom()+offY);

                movingXY(offX,offY);

                break;

            case MotionEvent.ACTION_UP:


                break;

        }
        return true;
    }

    public void movingXY(int offX,int offY){
        //移动View的关键代码
        layout(getLeft()+offX, getTop()+offY,    getRight()+offX    , getBottom()+offY);
    }


}

(二)布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xm


lns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >



    <com.example.myview.MyImageView
        android:id="@+id/view1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/view1"
        />



</LinearLayout>

(三)主方法的类

其实什么都不用写,显示出布局就可以了!

public class MainActivity extends Activity  {

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
}
}

第二个程序:

(一)自定义View的类1

package com.example.myview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * 自定义的View
 */

public class MyImageView extends ImageView {

    private int lastX;
    private int lastY;

    public MyImageView(Context context) {
        super(context);

    }

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }



    public boolean onTouchEvent(MotionEvent event) {

        //获取到手指处的横坐标和纵坐标
        int x = (int) event.getX();
        int y = (int) event.getY();

        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN://手指按下时
                lastX = x;
                lastY = y;
                break;
            case MotionEvent.ACTION_MOVE://手指移动时
                //计算移动的距离
                int offX = x - lastX;
                int offY = y - lastY;
                //调用layout方法来重新放置它的位置
                Log.e("TAG","距离左边="+getLeft()+offX+",距离顶部="+getTop()+offY+",距离右边="+ getRight()+offX+",距离底部="+ getBottom()+offY);

                movingXY(offX,offY);
                //listenerData.getDataXY(offX,offY);
                if (listenerData!=null){
                    listenerData.getDataXY(offX,offY);
                }
                break;

            case MotionEvent.ACTION_UP:


                break;

        }
        return true;
    }

    public void movingXY(int offX,int offY){
        //移动View的关键代码
        layout(getLeft()+offX, getTop()+offY,    getRight()+offX    , getBottom()+offY);
    }


    //监听接口的定义,用来获得数据
    interface  ListenerData{
        void getDataXY(int offX,int offY);
    }

    //定义一个接口对象
    ListenerData listenerData;


}

这个自定义View比上面一个就是多了一个回调接口!
并且移动时判断回调对象是否为空,不为空就返回数据。

(二)自定义View的类2

package com.example.myview;

import android.content.Context;
import android.util.AttributeSet;

/**
 * 第一个视图
 */

public class MyImageView1 extends MyImageView {


    public MyImageView1(Context context) {
        super(context);
    }

    public MyImageView1(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}

(三)自定义View的类3

package com.example.myview;

import android.content.Context;
import android.util.AttributeSet;

/**
 * 第二个视图
 */

public class MyImageView2 extends MyImageView {


    public MyImageView2(Context context) {
        super(context);
    }

    public MyImageView2(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}

上面两个指定View有相同的继承,因为要判断是哪一个视图的点击和移动事件,所以分开两个对象。

(四)布局文件

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


    <View
        android:layout_width="20dp"
        android:layout_height="30dp"
        />
    <com.example.myview.MyImageView1
        android:id="@+id/view1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/view1"
        />

 <View
    android:layout_width="20dp"
    android:layout_height="30dp"
    />

    <com.example.myview.MyImageView2
        android:id="@+id/view2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/view2"
        />




</LinearLayout>

(五)主方法类的设计

package com.example.myview;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;


public class MainActivity extends Activity  {

    MyImageView1 imageView1;
    MyImageView2 imageView2;

    boolean connect12;//连接模块12,模块1在上面
    boolean connect21;//连接模块21,模块2在上面

    int clickView=0;//点击的是哪一个模块

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView1= (MyImageView1) findViewById(R.id.view1);
        imageView2= (MyImageView2) findViewById(R.id.view2);


    //这里每个监听对象对应一个回调的方法
       MyImageView1.ListenerData listenerData=new MyImageView1.ListenerData() {
           @Override
           public void getDataXY(int offX, int offY) {
               Log.e("TAG","点击的视图:"+1111+"");
               clickView=1;
               if ((imageView2.getTop()-imageView1.getBottom())<20&&(imageView2.getTop()-imageView1.getBottom())>0&&Math.abs(imageView1.getLeft()-imageView2.getLeft())<30){
                 int y=imageView1.getBottom()-imageView2.getTop();

                   connect12=true;
               }

               if (connect12){
                   imageView2.movingXY(offX,offY);
               }

               if ((imageView1.getTop()-imageView2.getBottom())>20){
                   connect21=false;
               }

           }
       };

        MyImageView2.ListenerData listenerData2=new MyImageView2.ListenerData() {
            @Override
            public void getDataXY(int offX, int offY) {
                clickView=2;
                Log.e("TAG", "点击的视图:" + 22 + "");

                if ((imageView2.getTop()-imageView1.getBottom())>20){
                    connect12=false;
                }

                if ((imageView1.getTop()-imageView2.getBottom())<20&&(imageView1.getTop()-imageView2.getBottom())>0&&Math.abs(imageView1.getLeft()-imageView2.getLeft())<30){
                    connect21=true;
                }

                if (connect21){
                    imageView1.movingXY(offX,offY);
                }


            }


    };


        //实例化View中的回调对象,示例化之后才能再自定义类中对应的方法得到回调
         imageView1.listenerData=listenerData;
        imageView2.listenerData=listenerData2;

    }


}

这里图解一下相对位置的关系:

getLeft(),getTop(),getRight(),getButtom()
视图左侧位置 view.getLeft()
视图右侧位置 view.getRight()
视图顶部位置 view.getTop();
视图底部位置 view.getBottom();
视图宽度 view.getWidth();
视图高度 view.getHeight()
x3

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Android自定义View圆形刻度在实现上相对简单,主要步骤如下: 1. 创建一个继承自View自定义View类,命名为CircleScaleView。 2. 在该自定义View的构造方法中完成必要的初始化工作,例如设置画笔、设置View的宽高、设置绘制模式等。 3. 重写onMeasure()方法,设置View的尺寸大小。可以根据自定义的需求来决定View的宽高。 4. 重写onDraw()方法,完成绘制整个圆形刻度的逻辑。 5. 在onDraw()方法中,首先通过getMeasuredWidth()和getMeasuredHeight()方法获取到View的宽高,然后计算圆心的坐标。 6. 接着,使用Canvas对象的drawArc()方法来绘制圆弧,根据需求设置圆弧的起始角度和扫描角度。 7. 再然后,通过循环绘制每个刻度线,可以使用Canvas对象的drawLine()方法来绘制。 8. 最后,根据需要绘制刻度值或其他其他附加元素,例如圆心的标记。 9. 至此,整个圆形刻度的绘制逻辑就完成了。 10. 在使用该自定义View的时候,可以通过添加该View到布局文件中或者在代码中动态添加,并按需设置相应的属性。 需要注意的是,自定义圆形刻度的具体样式和行为取决于项目需求,上述步骤仅为基础实现框架,具体细节需要根据实际情况进行相应的调整。 ### 回答2: 在Android实现一个圆形刻度的自定义View有几个步骤。 首先,我们需要创建一个自定义View类,继承自View或者它的子类(如ImageView)。 接下来,在自定义View的构造方法中,初始化一些必要的属性,比如画笔的颜色、宽度等。我们可以使用Paint类来设置这些属性。 然后,我们需要在自定义View的onMeasure方法中设置View的宽度和高度,确保View在屏幕上正常显示。一种常见的实现方式是将宽度和高度设置为相同的值,使得View呈现出圆形的形状。 接着,在自定义View的onDraw方法中,我们可以利用画笔来绘制圆形刻度。可以使用canvas.drawCircle方法来绘制一个圆形,使用canvas.drawLine方法绘制刻度线。我们可以根据需要,定义不同的刻度颜色和宽度。 最后,我们可以在自定义View的其他方法中,添加一些额外的逻辑。比如,在onTouchEvent方法中处理触摸事件,以实现拖动刻度的功能;在onSizeChanged方法中根据View的尺寸调整刻度的大小等等。 当我们完成了自定义View的代码编写后,我们可以在布局文件中使用这个自定义View。通过设置布局文件中的属性,可以进一步自定义View的外观和行为。 总之,实现一个圆形刻度的自定义View,我们需要定义一个自定义View类,并在其中使用画笔来绘制圆形和刻度。通过处理一些事件和属性,我们可以实现更多的功能和样式。以上就是简单的步骤,可以根据需要进行更加详细的实现。 ### 回答3: Android自定义View圆形刻度可以通过以下步骤实现。 首先,我们需要创建一个自定义View,继承自View类,并重写onDraw方法。在该方法中,我们可以自定义绘制的内容。 其次,我们需要定义一个圆形的刻度尺背景,可以使用Canvas类提供的drawCircle方法来绘制实心圆或空心圆。 接着,我们可以通过Canvas类的drawLine方法来绘制刻度线。根据刻度的数量,可以计算出每个刻度之间的角度,然后循环绘制出所有的刻度线。 然后,我们可以通过Canvas类的drawText方法来绘制刻度的值。根据刻度线的角度和半径,可以计算出刻度的坐标,然后将刻度的值绘制在指定的位置上。 最后,我们可以通过在自定义View的构造方法中获取相关的参数,如刻度的最大值、最小值、当前值等,然后根据这些参数来计算刻度的位置和值。 在使用自定义View时,可以通过设置相关的属性来改变刻度的样式和位置。例如,可以设置刻度线的颜色、粗细、长度等,也可以设置刻度值的颜色、大小等。 通过以上步骤,我们就可以实现一个圆形刻度尺的自定义View。在使用时,可以根据需要自行调整绘制的样式和逻辑。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

峥嵘life

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值