日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/21 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自绘制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();}@Overridepublic 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);}@Overridepublic 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;} }

?

轉載于:https://www.cnblogs.com/wjhblogs/p/4698654.html

總結

以上是生活随笔為你收集整理的自绘制View---------左右滑动刻度调频View的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。