无法在 Android 自定义视图中以编程方式设置参数

Unable to programatically set parameter in a Android custom view

我已经构建了一个圆形进度条,除了一个百分比参数并生成一个进度条视图。

public class Myshape2 extends View {
    private Paint paint,paint2,paint3,paint4;
    private float percentage;
    float fangle;
    ObjectAnimator ob,ob2;
    Bitmap b,newb;

    public float getfX()
    {
        return fangle;
    }

    public void setPercentage(float percentage) {
        Float p=(360/100)*percentage;
        ob=ObjectAnimator.ofFloat(this,"fx",0f,p);
        ob.setDuration(2000);
        ob2=ObjectAnimator.ofFloat(this,"fp",0f,percentage);
        ob2.setDuration(2000);
        AnimatorSet a=new AnimatorSet();
        a.playTogether(ob,ob2);
        a.start();
    }

    public float getPercentage() {
       return percentage;
    }

    public void setFx(float value) {
        this.fangle = value;
        invalidate();
    }

    public void setFp(float value) {
        this.percentage = value;
        invalidate();
    }

    public Myshape2(Context context,AttributeSet st) {
        super(context,st);
        TypedArray a = context.getTheme().obtainStyledAttributes(
                st,
                R.styleable.Myshape2,
                0, 0);
        try {

            percentage = a.getFloat(R.styleable.Myshape2_percentage, 0);
        } finally {
            a.recycle();
        }
        paint=new Paint();
        paint2=new Paint();
        paint3=new Paint();
        paint4=new Paint();
        paint4.setTextSize(50);
        paint.setAntiAlias(true);
        paint.setARGB(255, 176, 176, 176);
        paint2.setARGB(255,255,255,255);
        paint3.setARGB(255,20,255,20);
        b= BitmapFactory.decodeResource(getResources(),R.drawable.drop);
        newb=Bitmap.createScaledBitmap(b,100,100,false);
        setPercentage(percentage);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        RectF rec=new RectF(170,170,470,470);
        canvas.drawCircle(320,320,150,paint);
        canvas.drawArc(rec,-90f,fangle,true,paint3);
        canvas.drawCircle(320,320,100,paint2);
        canvas.drawBitmap(newb,270,270,paint4);
        canvas.drawText(String.format("%.0f", percentage)+" %",270,520,paint4);
        //update();
       // invalidate();
    }

}

我的 xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:id="@+id/rl"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/percentage"
        android:hint="Enter Percentage"
        android:layout_gravity="center_horizontal" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ok"
        android:id="@+id/ok" />

    <com.example.pulkittyagi.draw_canvas.Myshape2
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:custom="http://schemas.android.com/apk/res/com.example.pulkittyagi.draw_canvas"
        android:id="@+id/gv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        custom:percentage="75"/>
</LinearLayout>

我想在 Mainactivity.java 中以编程方式设置百分比,但 setter 方法不可用。

public class MainActivity extends Activity {
    ImageView image;
    View gv ;
    EditText percentage;
    Button ok;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setup();
    }

    public void setup()
    {
        percentage=(EditText) findViewById(R.id.percentage);
        ok=(Button) findViewById(R.id.ok);
        gv=(Myshape2) findViewById(R.id.gv);
        gv.setPercentage(56); // can not be resolved
    }
}

更改 gv 的类型

发件人:

View gv;

收件人:

Myshape2 gv;