簡體   English   中英

如何刷新布局中的自定義視圖

[英]How to refresh a customized view in a layout

我剛剛創建了一個用於顯示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)),則該視圖將成功無效。 如何在整個布局中刷新視圖。

我沒看到問題。 這些是我通過您的代碼獲得的結果(下一次,您將執行此部分)。 使用setContentView(R.layout.pdfview) ,結果如下:

setContentView(R.layout.pdfview)

使用setContentView(new PDFGraphic(this, null) -請注意,由於您未定義PDFGraphic(Context),因此我使用了兩個參數的構造函數-這是結果:

新的PDFGraphic()

無論哪種情況,您的onDraw()都正確發生。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM