我使用CameraPreview作为Acitivity的背景,它还应该包含一些像Button和ImageView这样的元素。按钮单击使动画在ImageView内部启动。我设法添加并显示Button,但如果我添加ImageView应用程序崩溃。请帮忙。
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="30dp"
android:background="@drawable/playselector" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@android:color/transparent"
android:src="@drawable/anim" />
</RelativeLayout>
MainActivity.java:
public class MainActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
AnimationDrawable mAnim;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCamera = getCameraInstance();
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
/* ImageView image = (ImageView)findViewById(R.id.image);
image.setImageResource(R.anim.blanim);
mAnim = (AnimationDrawable)image.getBackground(); */ this part make app crash
Button button1 = (Button) this.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAnim.start();
}
});
}
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open();
}
catch (Exception e){
}
return c;
}
}
答案 0 :(得分:0)
而不是使用image.setImageResource(R.anim.blanim);
使用image.setBackgroundResource(R.anim.blanim);
获取更多信息,请查看here。