반응형

 

 

 

< MainActivity.java >

package com.example.testpage;

import android.content.Intent;
import android.graphics.Matrix;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ImageView imageView;
    Button translatingBtn, ScalingBtn, RotationBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.image_view);
        translatingBtn = findViewById(R.id.translating_btn);
        ScalingBtn = findViewById(R.id.scaling_btn);
        RotationBtn = findViewById(R.id.rotation_btn);

        translatingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Matrix matrix = new Matrix();
                imageView.setX(imageView.getX());
                imageView.setY(imageView.getY());
                matrix.postTranslate(imageView.getPivotX(), imageView.getPivotY());

                imageView.setImageMatrix(matrix);
            }
        });

        ScalingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Matrix matrix = imageView.getImageMatrix();
                imageView.setImageResource(R.drawable.ic_launcher_background);
                matrix.setScale(3f, 3f);
            }
        });

        RotationBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Matrix matrix = imageView.getImageMatrix();
                imageView.setRotation(imageView.getRotation() + 10);
                matrix.postRotate( imageView.getRotation(), imageView.getWidth() / 2, imageView.getHeight() / 2);
                imageView.setImageMatrix(matrix);
            }
        });
    }
}

 

 

 

< activity_main.xml >

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:orientation="vertical"
        android:gravity="center">

        <ImageView
            android:id="@+id/image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="matrix"
            android:layout_gravity="center_horizontal"
            android:src="@mipmap/ic_launcher_round"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center">
        <Button
            android:id="@+id/translating_btn"
            android:text="translating"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:text="scaling"
            android:id="@+id/scaling_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:text="rotation"
            android:id="@+id/rotation_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

 

 

반응형