• Android 画直线并实现拖动


    自定义View,在onDraw()方法中绘制一条直线,在onTouch()方法中监听手指的移动。

    public class AroundDragView extends View implements View.OnTouchListener {
        private static final String TAG = "ImageLight";
        protected int screenWidth;
        protected int screenHeight;
        protected int lastX;
     
        protected Paint paint = new Paint();
        int offset = 30;public AroundDragView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            setOnTouchListener(this);
        }
    
        public AroundDragView(Context context, AttributeSet attrs) {
            super(context, attrs);
            setOnTouchListener(this);
        }
    
        public AroundDragView(Context context) {
            super(context);
            setOnTouchListener(this);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            paint.setColor(Color.YELLOW);
            paint.setStrokeWidth(4.0f);
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawLine(offset / 2, 0, offset / 2, getHeight(), paint);
        }
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    oriLeft = v.getLeft();
                    oriRight = v.getRight();
                    lastX = (int) event.getRawX();
                    break;
                case MotionEvent.ACTION_MOVE:
                    int dx = (int) event.getRawX() - lastX;
                    move(v, dx);
                    lastX = (int) event.getRawX();
                    break;
                case MotionEvent.ACTION_UP:
                    break;
            }
            invalidate();
            return false;
        }
    
        /**
         * 移动
         *
         * @param v
         * @param dx
         */
        private void move(View v, int dx) {
            int left = v.getLeft() + dx;
            int right = v.getRight() + dx;
            if (left < 100 + offset) {
                left = 100 + offset;
                right = left + v.getWidth();
            }
            if (right > screenWidth - 100) {
                right = screenWidth - 100;
                left = right - v.getWidth();
            }
            v.layout(left, 0, right, getHeight());
        }
    
    }

    将上面的View作为一个View放在XML文件中即可,例如:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center">
    
        <com.desmand.utils.AroundDragView
            android:id="@+id/video_around_drag_view"
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:clickable="true"/>
    </RelativeLayout>

    参考:http://www.cnblogs.com/a284628487/p/3367228.html

  • 相关阅读:
    C#获取当前程序运行路径的方法集合
    SQL为查询的结果加上序号(ROW_NUMBER) 合并多个查询结果
    Asp.net导出excel时长数字被科学计数法的解决方案。(身份证长数字作为字符处理)
    Stopwatch的用法
    对web.config的ConnectionString加密
    如何检索数据库中的空值和null
    如何将闲置的平板作为第二显示器(分屏)使用
    pyinstaller 打包文件太大
    如何让openssl生成的SSL证书被浏览器认可
    还是那该死的IE~~~
  • 原文地址:https://www.cnblogs.com/fansen/p/5056048.html
Copyright © 2020-2023  润新知