surfaceViewのクラスです
java
1import static android.content.ContentValues.TAG; 2public class AnimationSurfaceView extends SurfaceView implements Runnable,SurfaceHolder.Callback{ 3 4 5 Canvas canvas = null; 6 7 SurfaceHolder surfaceHolder; 8 Thread thread; 9 int screen_width, screen_height; 10 11 public AnimationSurfaceView(Activity context) { 12 super(context); 13 14 Log.e(TAG,"AnimationSurfaceViewのコンストラクタが呼ばれた-"); 15 surfaceHolder = getHolder(); 16 surfaceHolder.addCallback(this); 17 surfaceHolder.setFormat(PixelFormat.TRANSLUCENT); 18 //setZOrderOnTop(true); 19 } 20 21 public AnimationSurfaceView(Context context, AttributeSet attrs) { 22 this(context, attrs, 0); 23 } 24 25 public AnimationSurfaceView(Context context, AttributeSet attrs, int defStyle) { 26 super(context, attrs, defStyle); 27 } 28 29 30 @Override 31 public void surfaceCreated(SurfaceHolder holder) { 32 thread = new Thread(this); 33 thread.start(); 34 Log.e(TAG,"surfaceCreated()"); 35 } 36 37 @Override 38 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 39 screen_width = width; 40 screen_height = height; 41 Log.e(TAG,"surfaceChanged"); 42 } 43 44 @Override 45 public void surfaceDestroyed(SurfaceHolder holder) { 46 thread = null; 47 Log.e(TAG,"surfaceDestroyed"); 48 } 49 50 @Override 51 public void run() { 52 Log.e(TAG,"runに入った"); 53 Paint paint = new Paint(); 54 Paint bgPaint = new Paint(); 55 // Background 56 bgPaint.setStyle(Paint.Style.FILL); 57 bgPaint.setColor(Color.WHITE); 58 59 try{ 60 canvas = surfaceHolder.lockCanvas(); 61 if(canvas!=null){ 62 Log.e(TAG,"canvasはnullじゃなかった"); 63 64 FaceLine faceLine=new FaceLine(); 65 Rect r=faceLine.getR(); 66 if(r!=null) { 67 Log.e(TAG, "Rect rがnullじゃない"); 68 } 69 drawFace(r); 70 }else if(canvas==null){ 71 Log.e(TAG,"canvasはnullだった"); 72 } 73 } 74 catch(Exception e){} 75 } 76 77 public void drawFace(Rect r) { 78 79 int color=Color.argb(255, 255, 0, 255); 80 Paint paint = new Paint(); 81 Log.e(TAG, "drawFaceが呼ばれた"); 82 try { 83 84 if (canvas != null) { 85 try { 86 Log.e(TAG, "canvasがnullじゃなかった"); 87 canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); 88 paint.setColor(color); 89 canvas.drawRect(r,paint); 90 91 } finally { 92 surfaceHolder.unlockCanvasAndPost(canvas); 93 } 94 } 95 } catch (IllegalArgumentException e) { 96 Log.w(TAG, e.toString()); 97 } 98 } 99}
Fragmentのクラスです。
java
1 2 @Override 3 public View onCreateView(LayoutInflater inflater, ViewGroup container, 4 Bundle savedInstanceState) { 5 return inflater.inflate(R.layout.fragment_camera2_basic, container, false); 6 } 7 8 @Override 9 public void onViewCreated(final View view, Bundle savedInstanceState) { 10 mTextureView = (AutoFitTextureView) view.findViewById(R.id.texture); 11 12 if(mTextureView==null){ 13 Log.e(TAG,"mTextureVieがnull"); 14 } 15 Log.e(TAG,"mSurfaceViewに値をセットした"); 16 mSurfaceView = (AnimationSurfaceView) view.findViewById(R.id.surface); 17 Log.e(TAG,"mSurfaceViewに値をセットした"); 18 mSurfaceView.setZOrderOnTop(true); 19 mSurfaceView = new AnimationSurfaceView(getActivity()); 20 if(mSurfaceView==null){ 21 Log.e(TAG,"mSurfaceViewがnull"); 22 } 23 }
xml
1<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent"> 4 5 6 <com.example.android.camera2basic.AutoFitTextureView 7 android:id="@+id/texture" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 android:layout_alignParentStart="true" 11 android:layout_alignParentTop="true" /> 12 13 14 <com.example.android.camera2basic.AnimationSurfaceView 15 android:id="@+id/surface" 16 android:layout_width="match_parent" 17 android:layout_height="match_parent" 18 19 /> 20 21</FrameLayout>
回答1件
あなたの回答
tips
プレビュー