如何刷新布局中的自定义视图

时间:2011-08-02 08:22:59

标签: android view invalidation

我刚创建了一个用于显示PDF文件的视图

    public class PDFGraphic extends View{
     String mText;
     float mLastX;
     float mLastY;
    public float mOffX;
    public float mOffY;
    Canvas mCan;
    Bitmap mBi;

    public PDFGraphic(Context context, AttributeSet attrs){ 
        super(context,attrs);
        setPageBitmap();
        setBackgroundColor(Color.TRANSPARENT);
    }
    public void uiInvalidate() {
        postInvalidate();
    }
    public void setPageBitmap() {
        mBi = Bitmap.createBitmap(100, 100, Config.RGB_565);
        mCan = new Canvas(mBi);
        mCan.drawColor(Color.RED);
        }
   public void onDraw(Canvas canvas) {
        Paint paint = new Paint();
             canvas.drawBitmap(mBi, 0, 0, paint);
        }
}

这是我的xml文件(pdfview):

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" android:orientation="vertical">
    <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="match_parent" android:gravity="bottom|center" android:layout_height="wrap_content" android:minHeight="30px" android:visibility="visible">
        <com.shawnprojectPDF.PDFGraphic android:id="@+id/pdfview1" android:layout_width="match_parent" android:layout_above="@+id/editText1" android:layout_alignParentTop="true" android:layout_height="match_parent"></com.shawnprojectPDF.PDFGraphic>
        <ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/leftarray" android:id="@+id/left" android:layout_alignParentBottom="true" android:layout_marginLeft="68px"></ImageButton>
        <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/left" android:layout_alignParentBottom="true" android:gravity="center" android:width="100px" android:singleLine="true" android:maxLength="12"></EditText>
        <ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/right" android:src="@drawable/rightarray" android:layout_toRightOf="@+id/editText1" android:layout_alignParentBottom="true"></ImageButton>
    </RelativeLayout>
</LinearLayout>

在我的主要活动中,如果setContentView(R.Layout.pdfview),视图(PDFGraphic)将不会失效,如果setContentView(New PDFGraphic(this)),它会成功失效。 如何在整个布局中刷新视图。

1 个答案:

答案 0 :(得分:0)

我没有看到问题。这些是我用你的代码得到的结果(下次你做这部分)。使用setContentView(R.layout.pdfview),结果如下:

setContentView(R.layout.pdfview)

使用setContentView(new PDFGraphic(this, null) - 请注意我使用了两个arg构造函数,因为你没有定义PDFGraphic(Context) - 这就是结果:

new PDFGraphic()

在任何一种情况下,您的onDraw()都正确发生。