안드로이드

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

class MainActivity

package org.androidtown.mycamera;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

 

R.layout.activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="org.androidtown.mycamera.MainActivity">

    <org.androidtown.mycamera.MyImageView
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

 

 

 class MyImageView

package org.androidtown.mycamera;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import static android.graphics.Bitmap.createBitmap;

/**
 * Created by choi on 2017-04-11.
 */

public class MyImageView extends View {

    Bitmap mBitmap;
    Canvas mCanvas;
    Context mContext;

    Bitmap image1;
    Paint paint;


    //카메라는 매트릭스와 같이 사용한다.
    //매트릭스 : 비트맵 객체를 매트릭스를 이용해 이미지를 변환할수 있게 한다.
    Camera camera =new Camera();


    public MyImageView(Context context) {
        super(context);
        init(context);
    }

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }



    private void init(Context context){
        mContext =context;

        paint  =new Paint();

        //저장된 이미지 불러오기 메모리에서 불러오는 것은 아니다. 단지 로딩하는 것이다.
        image1 = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.d1);
        // onSizeChange 가 호출되면 image1 객체를 메모리상 비트맵에 그린다.


    }


    //
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

         //실제로 뷰에 그리기
        if(mBitmap!=null){
            // 메모리에 비트맵 객체가 존재하면
            // 캔버스에 그린다.
            canvas.drawBitmap(mBitmap, 0 ,0,  null);

        }

    }



    //화면이 정해졌을 때 메모리상의 비트맵 이미지 만들기
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {

        if(w > 0 && h >0){
            mBitmap= createBitmap(w, h, Bitmap.Config.ARGB_8888);
            mCanvas =new Canvas(mBitmap);
            mCanvas.setBitmap(mBitmap);
            mCanvas.drawColor(Color.WHITE);

            // init 에서 불러온 iamge1 을 것을 메모리상 비트맵에 실제로 그린다.
            // 1. 첫번째 이미지
            mCanvas.drawBitmap(image1, 0, 0, paint);



            // 2번째 카메라  3D 변형
            //카메라를 사용해서 3D 적용
            // 먼저 bitMap 을 변형시킬수 있는 Matrix 를 사용한다.
            camera.save();
            Matrix matrix =new Matrix();
            camera.rotateY(40.0f);
            camera.translate(0.0f, 0.0f, -3000.0f);
            camera.getMatrix(matrix);
            camera.restore();

            // 카메라가 적용된 매트릭스를 이용해서  새롭게 비트맵 생성
            Bitmap  rotatedFlower=Bitmap.createBitmap(image1, 0, 0, image1.getWidth(), image1.getHeight(), matrix, true);
            mCanvas.drawBitmap(rotatedFlower, 0, 300, paint);
        }

    }






}









 

 

 

 

 

77강

 

 

 

78강

 

 

 

about author

PHRASE

Level 60  머나먼나라

인민, 오직 인민만이 세계역사를 만드는 원동력이다. -모택동

댓글 ( 4)

댓글 남기기

작성