• 自绘制View---------左右滑动刻度调频View


    代码中使用自定义View:

    private SeekView mSeekView = null;
    ......
    mSeekView = new SeekView(this, mHandler);
    RelativeLayout rela = (RelativeLayout)findViewById(R.id.seek_part);
    rela.addView(mSeekView);

    重写View如下:

    package com.mediatek.fmradio;
    
    import java.util.StringTokenizer;
    
    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.content.res.Resources;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.LinearGradient;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.graphics.Paint.Align;
    import android.graphics.Path;
    import android.graphics.Shader;
    import android.graphics.Typeface;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.text.format.Time;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.SeekBar;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class SeekView extends View {
    
        private static final String TAG = "SeekView";
    
        private final Paint mPaint = new Paint();
        private final double FM_STATION_FIELD = 108.0 - 87.5;
        private final double FM_STATION_MIN = 87.5;
        private final double FM_STATION_MAX = 108.0;
        private Bitmap mBitmapSlider;
        private Bitmap mBitmapSliderPress;
        private String mStrStationValue;
        private float mFloatStationValue;
        private float mOffSet;
        private float mSliderLength;
        private float mSliderMargin;
        private boolean mBoolClickPress;
        private Handler mHandler;
        Message mMessage;
        Bundle mBundle;
    
        public SeekView(Context context) {
            super(context);
        }
    
        public SeekView(Context context, final Handler mHandler) {
            super(context);
            this.mHandler = mHandler;
            mBundle = new Bundle();
            mSliderLength = 0;
            mSliderMargin = (float) context.getResources().getDimension(R.dimen.slider_margin);
            mBitmapSlider = BitmapFactory.decodeResource(context.getResources(), R.drawable.indicator);
            mBitmapSliderPress = BitmapFactory.decodeResource(context.getResources(), R.drawable.indicator_pressed);
            if (null != mBitmapSlider) {
                mSliderMargin = mSliderMargin + mBitmapSlider.getWidth() / 2;
            }
            mBoolClickPress = false;
            mStrStationValue = "87.5";
            mFloatStationValue = 87.5f;
        }
    
        public static float dip2px(Context context, float dpValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (dpValue * scale + 0.5f);
        }
    
        public static float px2dip(Context context, float pxValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (pxValue / scale + 0.5f);
        }
    
        public SeekView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public SeekView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        public String getStationValue() {
            return this.mStrStationValue;
        }
    
        public float getFloatStationValue() {
            return this.mFloatStationValue;
        }
    
        public void setStationValue(String st_v) {
            this.mStrStationValue = st_v;
            this.mFloatStationValue = Float.parseFloat(mStrStationValue);
            updateSeekView();
        }
    
        public void setStationValue(float st_v) {
            this.mFloatStationValue = st_v;
            this.mStrStationValue = Float.toString(this.mFloatStationValue);
            updateSeekView();
        }
    
        public void updateSeekView() {
            this.invalidate();
        }
    
        @Override
        public void onDraw(Canvas canvas) {
            float viewWidth = (float) getWidth();
            if (viewWidth == 0) {
                return;
            }
            if (mFloatStationValue < FM_STATION_MIN || mFloatStationValue > FM_STATION_MAX) {
                return;
            }
            mSliderLength = getWidth() - mSliderMargin * 2;
            mOffSet = (float) ((mFloatStationValue - 87.5) * mSliderLength / (108.0 - 87.5));
    
            canvas.drawBitmap(mBoolClickPress ? mBitmapSliderPress : mBitmapSlider,
                    mSliderMargin - mBitmapSlider.getWidth() / 2 + mOffSet, getTop(), mPaint);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float mX = event.getX();
            float mY = event.getY();
            if (mY < getTop() || mY > getBottom()) {
                return false;
            }
            mSliderLength = getWidth() - mSliderMargin * 2;
            mOffSet = (float) ((mFloatStationValue - 87.5) * mSliderLength / (108.0 - 87.5));
            if (mY > getTop() && mY < (getTop() + mBitmapSlider.getHeight())
                    && mX > (mSliderMargin + mOffSet - mBitmapSlider.getWidth() / 2)
                    && mX < (mSliderMargin + mOffSet + mBitmapSlider.getWidth() / 2)) {
                mBoolClickPress = true;
            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                mBoolClickPress = false;
            }
            if (mX < mSliderMargin) {
                mOffSet = 0;
            } else if (mX > (getRight() - mSliderMargin)){
                mOffSet = mSliderLength;
            } else {
                mOffSet = mX - mSliderMargin;
            }
            mFloatStationValue = (float) (mOffSet * (108.0 - 87.5) / mSliderLength + 87.5);
    
            mBundle.putInt(FmRadioListener.KEY_STATION_VALUE, (int) ((mFloatStationValue) * 10));
            mMessage = new Message();
            mMessage.what = FmRadioListener.MSGID_MOVE_FINISHED;
            mMessage.setData(mBundle);
            mHandler.sendMessage(mMessage);
    
            this.invalidate();
            return true;
        }
    }
  • 相关阅读:
    【转】C#进阶系列——WebApi 接口参数不再困惑:传参详解
    微信内测小程序,苹果你怎么看?
    给你一个团队,你应该怎么管?
    ios修改产品名
    【原创】windows下搭建vue开发环境+IIS部署
    【原】“系统”重新启动
    Ubuntu root密码修改
    【转】网络编程常见问题总结
    Python + Selenium -Python 3.6 3.7 安装 PyKeyboard PyMouse
    python3 获取当前路径及os.path.dirname sys.path.dirname的使用
  • 原文地址:https://www.cnblogs.com/wjhblogs/p/4698654.html
Copyright © 2020-2023  润新知