我正努力在我正在编码的Android练习应用中的相机预览活动上实现像动画(上下)这样的扫描仪线。
我已经成功地使动画代码在单个活动中工作 - 没有相机预览。无论我如何尝试在stackoverflow上搜索或搜索网络的所有内容,我总是得到相机预览,但不是动画叠加。
这是我的xml文件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@android:color/transparent"
android:layout_centerInParent="true">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:src="@drawable/scanline"
android:contentDescription="@string/scanline"
android:layout_gravity="left|center_vertical"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:background="@android:color/transparent"
android:baselineAlignBottom="false"/>
<com.app.arsinoe.ui.widget.CameraPreview
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent" />
<Button
android:layout_width="156dp"
android:layout_height="27dp"
android:text="@string/exit_button"
android:id="@+id/aboardButton2"
android:layout_gravity="bottom|left"
android:clickable="true"
android:background="#4e0612"
android:textColor="@android:color/white"
android:textStyle="bold"
android:alpha="1"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:paddingStart="@dimen/activity_vertical_margin"
android:onClick="exitButton"/>
<Button
android:layout_width="310dp"
android:layout_height="27dp"
android:text="@string/scan_button"
android:id="@+id/scanButton"
android:layout_gravity="bottom|right"
android:clickable="true"
android:textColor="@android:color/white"
android:textStyle="bold"
android:alpha="1"
android:background="#1b4a6c"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:paddingStart="@dimen/activity_vertical_margin"
android:onClick="scanButton"/>
</FrameLayout>
这是我的扫描仪线动画活动的工作代码:
public class ScanActivity extends Activity{
private View view;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.scan_activity);
final Context readySnanner = this;
Handler mHandler = new Handler();
Runnable
makeToast = new Runnable() {
public void run() {
Toast.makeText(readySnanner,
"Bla bla bla...", Toast.LENGTH_LONG).show();
}
};
mHandler.postDelayed(makeToast, 4000);
Toast.makeText(ScanActivity.this,"Bla bla... Please wait", Toast.LENGTH_SHORT).show();
ImageView animatedImage = (ImageView) findViewById(R.id.imageView);
Animation animation
= AnimationUtils.loadAnimation(this, R.anim.topdown);
if (animation != null) {
animatedImage.startAnimation(animation);}
final int SPLASH_TIME;// 4 seconds
SPLASH_TIME = 4 * 1000;
boolean b;
b = new Handler().postDelayed(new Runnable() {
@Override
public void run() {
}
}, SPLASH_TIME);
}
public void exitButton(View view) {
this.finish();
System.exit(0);
}
public void scanButton(View view) {
Intent intent = new Intent(ScanActivity.this,
B.class);
startActivity(intent);
ScanActivity.this.finish();
}
这是我的CameraActivity工作代码:
public class CameraActivity extends Activity {
private Camera camera;
private View view;
SurfaceView animatedImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scan_activity);
setResult(RESULT_CANCELED);
// Camera may be in use by another activity or the system or not available at all
try {
camera = getCameraInstance();
} catch (Exception e) {
e.printStackTrace();
}
if(cameraAvailable(camera)){
initCameraPreview();
} else {
finish();
}
}
// Show the camera view on the activity
private void initCameraPreview() {
CameraPreview cameraPreview = (CameraPreview) findViewById(R.id.camera_preview);
cameraPreview.init(camera);}
我不知道如何使用提供的xml文件将这两个活动作为单个活动一起运行。请帮助新手,因为我被困在这些问题上5天了!
我很抱歉没有提供图像来描绘我正在尝试编码的内容,但我的声誉很小。任何回应,最好带有工作样本,我们将不胜感激。
修改
下图描绘了活动的布局:
红线应该在动画中上下移动,超过相机预览。这是在ImageView中编码和工作,但不适用于CameraPreview。
这是我的CameraPreview活动,编辑为Juan-devtopia.coop建议:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private Context context;
private Camera camera;
public CameraPreview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CameraPreview(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CameraPreview(Context context, Camera camera) {
super(context);
this.context = context;
this.camera = camera;
}
public void init(Camera camera) {
this.camera = camera;
initSurfaceHolder();
}
@SuppressWarnings("deprecation") // needed for < 3.0
private void initSurfaceHolder() {
SurfaceHolder holder = getHolder();
if (holder != null) {
holder.addCallback(this);
}
if (holder != null) {
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
initCamera(holder);
}
private void initCamera(SurfaceHolder holder) {
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (Exception ignored) {
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
public void setContext(Context context) {
this.context = context;
}
public class Animation extends Activity {
ImageView animatedImage = (ImageView) findViewById(R.id.imageView);
android.view.animation.Animation animation;
{
animatedImage.startAnimation(animation);
animation = AnimationUtils.loadAnimation(this, R.anim.topdown);
}
}
编译并在设备上运行时,在logcat中我得到一个“java.lang.RuntimeException:无法实例化活动”,由“引起:java.lang.InstantiationException:com.app.myapp.ui.widget”引起。 CameraPreview。有什么建议吗?
答案 0 :(得分:1)
将ImageView
放置在与预览相同的布局中,以便在其上方呈现。然后将两个代码放在同一个活动中,即初始化相机预览的代码和为ImageView
将CameraPreview
视为您布局中的另一个View
。
换句话说,你初始化你的相机预览,就像你的代码一样,但也在那里开始动画,让你的init相机方法如下:
// Show the camera view on the activity and show overlay animation
private void initCameraPreview() {
CameraPreview cameraPreview = (CameraPreview) findViewById(R.id.view);
cameraPreview.init(camera);
ImageView animatedImage = (ImageView) findViewById(R.id.imageView);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
animatedImage.startAnimation(animation);
}
请注意,我更改了id和动画,因为我必须重新创建它来测试它。布局只包含曲面和图像视图,将imageView进一步放在代码中(意味着它将在上渲染表面视图)。
在我的代码示例中(我将它上传到gitHub为你here)ic_launcher只是在相机预览上旋转,但我假设使用你的动画并确保它循环而不仅仅是动画一次,应该有效。