android – 用于控制音量的自定义旋钮视图?

android – 用于控制音量的自定义旋钮视图?,第1张

概述我想在旋钮周围显示进度条.按照本教程后我创建了这个 旋钮它工作正常. 但是我怎么能修改上面的旋钮看起来像第二个图像 第一个旋钮的运行代码如下所示. import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics 我想在旋钮周围显示进度条.按照本教程后我创建了这个
旋钮它工作正常.

但是我怎么能修改上面的旋钮看起来像第二个图像

第一个旋钮的运行代码如下所示.

import androID.content.Context;import androID.graphics.Bitmap;import androID.graphics.BitmapFactory;import androID.graphics.Canvas;import androID.graphics.color;import androID.graphics.Matrix;import androID.graphics.Paint;import androID.graphics.RectF;import androID.vIEw.GestureDetector;import androID.vIEw.GestureDetector.OnGestureListener;import androID.vIEw.VIEw.MeasureSpec;import androID.vIEw.MotionEvent;import androID.Widget.ImageVIEw;import androID.Widget.ImageVIEw.ScaleType;import androID.Widget.relativeLayout;public class RoundKnobbutton extends relativeLayout implements        OnGestureListener {    public int eventValue=10;    //doctory starts     Paint p1,p2,p3;    RectF oval;    int wIDth;    //doctory ends    private GestureDetector gestureDetector;    private float mAngleDown,mAngleUp;    private ImageVIEw ivRotor;    private Bitmap bmpRotorOn,bmpRotorOff;    private boolean mState = false;    private int m_nWIDth = 0,m_nHeight = 0;    public interface RoundKnobbuttonListener     {        public voID onStateChange(boolean newstate);        public voID onRotate(int percentage);    }    private RoundKnobbuttonListener m_Listener;    public voID SetListener(RoundKnobbuttonListener l)     {        m_Listener = l;    }    public voID SetState(boolean state)    {        mState = state;        ivRotor.setimageBitmap(state ? bmpRotorOn : bmpRotorOff);    }    public RoundKnobbutton(Context context,int back,int rotoron,int rotoroff,final int w,final int h) {        super(context);        //doctory starts         wIDth = w;        p1 = new Paint(1);        p1.setcolor(color.rgb(86,86,86));          p1.setStyle(androID.graphics.Paint.Style.FILL);         p2 = new Paint(1);         p2.setcolor(color.rgb(245,109,89));         p2.setStyle(androID.graphics.Paint.Style.FILL);         p3 = new Paint(1);         p3.setcolor(color.GREEN);         p3.setStyle(androID.graphics.Paint.Style.stroke);        oval = new RectF();        //doctory ends...        // we won't wait for our size to be calculated,we'll just store out        // fixed size        m_nWIDth = w;        m_nHeight = h;        // create stator        ImageVIEw ivBack = new ImageVIEw(context);        ivBack.setimageResource(back);        relativeLayout.LayoutParams lp_ivBack = new relativeLayout.LayoutParams(                w,h);        lp_ivBack.addRule(relativeLayout.CENTER_IN_PARENT);        addVIEw(ivBack,lp_ivBack);        // load rotor images        Bitmap srcon = BitmapFactory.decodeResource(context.getResources(),rotoron);        Bitmap srcoff = BitmapFactory.decodeResource(context.getResources(),rotoroff);        float scaleWIDth = ((float) w) / srcon.getWIDth();        float scaleHeight = ((float) h) / srcon.getHeight();        Matrix matrix = new Matrix();        matrix.postscale(scaleWIDth,scaleHeight);        bmpRotorOn = Bitmap.createBitmap(srcon,srcon.getWIDth(),srcon.getHeight(),matrix,true);        bmpRotorOff = Bitmap.createBitmap(srcoff,srcoff.getWIDth(),srcoff.getHeight(),true);        // create rotor        ivRotor = new ImageVIEw(context);        ivRotor.setimageBitmap(bmpRotorOn);        relativeLayout.LayoutParams lp_ivKnob = new relativeLayout.LayoutParams(                w,h);// LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);        lp_ivKnob.addRule(relativeLayout.CENTER_IN_PARENT);        addVIEw(ivRotor,lp_ivKnob);        // set initial state        SetState(mState);        // enable gesture detector        gestureDetector = new GestureDetector(getContext(),this);    }    /**     * math..     *      * @param x     * @param y     * @return     */    private float cartesiantopolar(float x,float y) {        return (float) -Math.todegrees(Math.atan2(x - 0.5f,y - 0.5f));    }    @OverrIDe    public boolean ontouchEvent(MotionEvent event) {        if (gestureDetector.ontouchEvent(event))            return true;        else            return super.ontouchEvent(event);    }    public boolean onDown(MotionEvent event)     {        float x = event.getX() / ((float) getWIDth());        float y = event.getY() / ((float) getHeight());        mAngleDown = cartesiantopolar(1 - x,1 - y);// 1- to correct our custom                                                    // axis direction        return true;    }    public boolean onSingleTapUp(MotionEvent e)    {        float x = e.getX() / ((float) getWIDth());        float y = e.getY() / ((float) getHeight());        mAngleUp = cartesiantopolar(1 - x,1 - y);// 1- to correct our custom                                                    // axis direction        // if we click up the same place where we clicked down,it's just a        // button press        if (!float.isNaN(mAngleDown) && !float.isNaN(mAngleUp)                && Math.abs(mAngleUp - mAngleDown) < 10) {            SetState(!mState);            if (m_Listener != null)                m_Listener.onStateChange(mState);        }        return true;    }    public voID setRotorPosAngle(float deg)    {        if (deg >= 210 || deg <= 150)         {            if (deg > 180)                deg = deg - 360;            Matrix matrix = new Matrix();            ivRotor.setScaleType(ScaleType.MATRIX);        //  matrix.postRotate((float) deg,210 / 2,210 / 2);// getWIDth()/2,// getHeight()/2);            matrix.postRotate((float) deg,m_nWIDth/2,m_nHeight/2);//getWIDth()/2,getHeight()/2);            ivRotor.setimageMatrix(matrix);        }    }    public voID setRotorPercentage(int percentage)    {        int posDegree = percentage * 3 - 150;        if (posDegree < 0)            posDegree = 360 + posDegree;        setRotorPosAngle(posDegree);    }    public boolean onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY) {        float x = e2.getX() / ((float) getWIDth());        float y = e2.getY() / ((float) getHeight());        float rotdegrees = cartesiantopolar(1 - x,1 - y);// 1- to correct our                                                            // custom axis                                                            // direction        if (!float.isNaN(rotdegrees)) {            // instead of getting 0-> 180,-180 0,we go for 0 -> 360            float posdegrees = rotdegrees;            if (rotdegrees < 0)                posdegrees = 360 + rotdegrees;            // deny full rotation,start start and stop point,and get a linear            // scale            if (posdegrees > 210 || posdegrees < 150) {                // rotate our imagevIEw                setRotorPosAngle(posdegrees);                // get a linear scale                float scaledegrees = rotdegrees + 150; // given the current                                                        // parameters,we go                                                        // from 0 to 300                // get position percent                int percent = (int) (scaledegrees / 3);                if (m_Listener != null)                    m_Listener.onRotate(percent);                return true; // consumed            } else                return false;        } else            return false; // not consumed    }    public voID onShowPress(MotionEvent e) {        // Todo auto-generated method stub    }    public boolean onFling(MotionEvent arg0,MotionEvent arg1,float arg2,float arg3) {        return false;    }    public voID onLongPress(MotionEvent e) {    }    /*@OverrIDe    protected voID onDraw(Canvas canvas)     {        super.onDraw(canvas);        int i = wIDth / 4;        oval.set(i - i / 2,i / 2,i * 3 + i / 2,i * 3 + i / 2);        canvas.drawoval(oval,p1);        canvas.drawArc(oval,-90F,(int)Math.round((360D * Double.valueOf(eventValue).doubleValue()) / 100D),true,p2);        canvas.drawline(20,30,120,200,p2);    }*/    /*@OverrIDe    protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec)    {        int desireDWIDth  = wIDth;        int desiredHeight = wIDth;        int wIDthMode  = androID.vIEw.VIEw.MeasureSpec.getMode(wIDthMeasureSpec);        int wIDthSize = androID.vIEw.VIEw.MeasureSpec.getSize(wIDthMeasureSpec);        int heightmode = androID.vIEw.VIEw.MeasureSpec.getMode(heightmeasureSpec);        int heightSize = androID.vIEw.VIEw.MeasureSpec.getSize(heightmeasureSpec);        int measureDWIDth;        int measuredHeight;        if (wIDthMode == MeasureSpec.EXACTLY)        {            measureDWIDth = wIDthSize ;        }        else if (wIDthMode == MeasureSpec.AT_MOST)        {            measureDWIDth = Math.min(desireDWIDth,wIDthSize);        }         else        {            measureDWIDth = desireDWIDth;        }        if (heightmode == MeasureSpec.EXACTLY)        {            measuredHeight = heightSize ;        }        else if (heightmode == MeasureSpec.AT_MOST)        {            measuredHeight = Math.min(desiredHeight,heightSize );        }        else        {            measuredHeight = desiredHeight;        }        setMeasuredDimension(measureDWIDth,measuredHeight);    }*/}

我创建了onDraw和onMeasure函数函数并尝试绘制外部进度,或者我如何修改它.

编辑

是否可以将旋转百分比更改为20.我的意思是它显示从0到99的进度.是否可以将其转换为0到12.

解决方法 使用两个XML文件.

circular_progress_drawable.xml

<?xml version="1.0" enCoding="utf-8"?><rotate xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:fromdegrees="270"    androID:todegrees="270">    <shape        androID:innerRadiusRatio="2.5"        androID:shape="ring"        androID:thickness="1dp">        <gradIEnt            androID:angle="0"            androID:endcolor="#22FA05"            androID:startcolor="#22FA05"            androID:type="sweep"            androID:useLevel="false" />    </shape></rotate>

和background_circle.xml

<?xml version="1.0" enCoding="utf-8"?><shape    xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:shape="ring"    androID:innerRadiusRatio="2.5"    androID:thickness="1dp"    androID:useLevel="false">    <solID androID:color="#000000" /></shape>

最后

<Progressbar        androID:ID="@+ID/progressbar"        androID:layout_wIDth="250dp"        androID:layout_height="250dp"        androID:indeterminate="false"        androID:progressDrawable="@drawable/circular_progress_drawable"        androID:background="@drawable/background_circle"                androID:max="100"        androID:progress="25" />

请注意,根据您拥有的图像位置设置宽度和高度

总结

以上是内存溢出为你收集整理的android – 用于控制音量的自定义旋钮视图?全部内容,希望文章能够帮你解决android – 用于控制音量的自定义旋钮视图?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1125375.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存