java无法在Android自定义视图中以编程方式设置参数
我已经构建了一个圆形progressbar,它除了一个百分比参数之外,还生成了一个progressbar视图
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:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
安卓:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
安卓:id="@+id/rl"
安卓:orientation="vertical">
<EditText
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:inputType="number"
安卓:ems="10"
安卓:id="@+id/percentage"
安卓:hint="Enter Percentage"
安卓:layout_gravity="center_horizontal" />
<Button
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Ok"
安卓:id="@+id/ok" />
<com.example.pulkittyagi.draw_canvas.Myshape2
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:custom="http://schemas.安卓.com/apk/res/com.example.pulkittyagi.draw_canvas"
安卓:id="@+id/gv"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginLeft="50dp"
custom:percentage="75"/>
</LinearLayout>
我想通过编程在Mainactivity中设置Percange。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
}
}
# 1 楼答案
改变gv的类型
发件人:
致: