[英]How to avoid calling bringToFront() in onDraw of custom view?
您可以在activity_main.xml
看到一個自定義視圖。 現在,它已隱藏在相機片段下。 我希望它顯示在它上面。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.snappiesticker.cwac4.StickerView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
這是類本身:
public class StickerView extends View {
Bitmap bitmap;
float x = 2;
float y = 2;
public StickerView(Context context, AttributeSet attrs) {
super(context,attrs);
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.paunch);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bitmap, x, y, null);
// I wanted to put it here but that would cause the loop described below.
}
}
通過循環,我的意思是:調用了onDraw
,它調用了bringToFront
,它要求重新渲染布局。 哪個調用onDraw
,哪個調用bringToFront
,等等。
這就是我要避免的。
這是在mainActivity
實例化的。
public class MainActivity extends Activity implements CameraHostProvider {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DemoCameraFragment current = new DemoCameraFragment();
getFragmentManager().beginTransaction()
.add(R.id.fragment_container, current)
.commit();
// can I somehow tell it to render the custom sticker view at the front here?
}
}
我是否正確理解,您想讓自定義的StickerView保留在相機片段的頂部? 如果是這樣的話,您也可以簡單地將布局調整到這樣的樣子;
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.snappiesticker.cwac4.StickerView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
使用此布局,您的相機片段將添加到StickerView下,並且根本不需要調用BringToFront() 。 如果您將根布局用作片段容器,則會在您的自定義視圖之后(即上方)添加片段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.