Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 用于控制音量的自定义旋钮视图?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在旋钮周围显示进度条.按照本教程后我创建了这个
旋钮它工作正常.

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

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

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)R_972_11845@ath.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://scheR_972_11845@as.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://scheR_972_11845@as.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"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"
        android:progress="25" />

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

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。