サーフェスビューがあり、サーフェスビューに線形レイアウトのコンテンツを「描画」したいと思います。
私の表面ビューはカメラのプレビューです。写真を撮るときに、レイアウトの内容が写真に描かれることを望みます。
これはどうですか?
どうも。
サーフェスビューがあり、サーフェスビューに線形レイアウトのコンテンツを「描画」したいと思います。
私の表面ビューはカメラのプレビューです。写真を撮るときに、レイアウトの内容が写真に描かれることを望みます。
これはどうですか?
どうも。
表面レイアウト:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="bottom"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/takepicture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/trans_tshirt"
android:contentDescription="tshirt" />
main.xml
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<SurfaceView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/camerapreview"/>
</LinearLayout>
java code:
package com.views;
import android.app.Activity;
import android.content.ContentValues;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.Toast;
import com.fashion.alex.R;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
public class HomeActivity extends Activity implements SurfaceHolder.Callback
{
private Camera camera;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private boolean previewing = false;
private LayoutInflater controlInflater = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.tshirt_layout, null);
LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
final ImageView ivButton = (ImageView)findViewById(R.id.takepicture);
ivButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
camera.takePicture(myShutterCallback,
myPictureCallback_RAW, myPictureCallback_JPG);
}}
);
}
Camera.ShutterCallback myShutterCallback = new Camera.ShutterCallback(){
@Override
public void onShutter() {
}
};
Camera.PictureCallback myPictureCallback_RAW = new Camera.PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
}
};
Camera.PictureCallback myPictureCallback_JPG = new Camera.PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
Uri uriTarget = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(HomeActivity.this,
"Image saved: " + uriTarget.toString(),
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}
};
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if(previewing){
camera.stopPreview();
previewing = false;
}
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
} catch (IOException exception) {
camera.release();
camera = null;
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}
以下のようなLayout/.xmlファイルを作成します
1)LinearLayout A 2)Linear Layout Aには、必要に応じてボタン/テキストの子ビューが必要です。
3)線形レイアウトAにはサーフェスビューBも必要です
アクティビティでこのsurfaceViewBを使用して、カメラのプレビューを表示します。このようにして、基本的に画面上に表面ビューやその他のビューを表示できます。