自定义ImageView无效无效

时间:2014-09-21 15:30:26

标签: android

我创建了自己的ImageView绘制形状.. 每个形状我也有4个单选按钮。 在提取时,方法检查检查哪个无线电并基于此绘制形状。 我希望ImageView在每次检查一个单选按钮时显示不同的形状 - 所以我在OnCheckedChangeListener上使它无效,但它没有重绘,只是保持不变。

    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);
            }
        }

    }


}

}

我不明白为什么它不令人耳目一新。请帮帮我

1 个答案:

答案 0 :(得分:0)

这不起作用,因为invalidate()仅调度主线程事件队列中的重绘。这意味着只有在当前代码全部执行完后才会执行重绘(在本例中为for循环)。

我有同样的问题,我让它运行,

runOnUiThread(new Runnable() {
    public void run() {
     imageView.invalidate();
    }
});