0
我創建了我自己的ImageView,它繪製形狀.. 我也有4個單選按鈕爲每個形狀。 在此基礎上提取方法檢查哪些收音機檢查並畫出形狀。 我希望ImageView在每次檢查一個單選按鈕時顯示不同的形狀,所以我在OnCheckedChangeListener上使其失效,但它不重繪,只是保持不變。自定義ImageView無效不工作
package com.example.androidshapes;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.*;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RadioGroup radio=(RadioGroup) findViewById(R.id.radioGroup);
final CustomImageView v= (CustomImageView) findViewById(R.id.c1);
OnCheckedChangeListener on= new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
runOnUiThread(new Runnable() {
public void run() {
v.invalidate();
}
});
}
};
radio.setOnCheckedChangeListener(on);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
這是我的主要活動
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidshapes.MainActivity" >
<com.example.androidshapes.CustomImageView
android:id="@+id/c1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<RadioButton
android:id="@+id/radioCircle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Circle" />
<RadioButton
android:id="@+id/radioRectangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Rectangle" />
<RadioButton
android:id="@+id/radioLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line" />
<RadioButton
android:id="@+id/radioTriangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Triangle" />
</RadioGroup>
,這是XML
public class CustomImageView extends ImageView
{
public CustomImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas)
{
Paint paint = new Paint(Paint.LINEAR_TEXT_FLAG);
paint.setColor(Color.BLACK);
paint.setTextSize(50.0F);
View view = View.inflate(this.getContext(), R.layout.activity_main, null);
RadioGroup radio=(RadioGroup) view.findViewById(R.id.radioGroup);
int id=radio.getCheckedRadioButtonId();
if(id==R.id.radioCircle)
{
Circle c=new Circle(new Point(this.getHeight()/2,this.getWidth()/2), 100,"BLACK", 5, "BLACK");
canvas.drawCircle((float) c.getCenter().getX(), (float) c.getCenter().getY(), (float) c.getRadius(), paint);
}
else
{
if(id==R.id.radioRectangle)
{
Rectangle r=new Rectangle(new Point(this.getHeight()/2,this.getWidth()/2), 200, 100, "Black", 5, "Black");
canvas.drawRect((float)r.topLeft().getX(), (float)r.topLeft().getY(), (float)r.bottomRight().getX(), (float)r.bottomRight().getY(), paint);
}
else
{
if(id==R.id.radioLine)
{
Line l=new Line(new Point(0,0), new Point(100,100),5, "Black");
canvas.drawLine((float)l.getP1().getX(), (float)l.getP1().getY(), (float)l.getP2().getX(), (float)l.getP2().getY(), paint);
}
}
}
}
}
我不明白爲什麼它不清爽。請幫我
感謝您的回答! 那麼我作爲setimagebitmap中的參數呢? 我沒有圖像。 以及當前代碼全部執行完成後的含義是什麼?代碼的哪一部分 – 2014-09-21 16:05:59
您可以設置新的更新圖像,而不是我的這一行 - >「myImageView.setImageBitmap(image)」,或者如果您不想設置,那麼只是使您的imageview無效。看到mu更新的答案。 – Beena 2014-09-21 16:08:06
它仍然無法正常工作,請參閱我的更新 – 2014-09-21 16:11:28