Computer >> Máy Tính >  >> Lập trình >> Android

Làm thế nào để xoay hình ảnh trong chế độ xem hình ảnh theo một góc trong Android?

Ví dụ này trình bày cách Xoay hình ảnh trong chế độ xem hình ảnh theo một góc.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   android:id="@+id/parent"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity"
   android:gravity="center"
   android:orientation="vertical">
   <ImageView
      android:id="@+id/imageView"
      android:src="@mipmap/ic_launcher"
      android:layout_width=" wrap_content"
      android:layout_height=" wrap_content" />
   <TextView
      android:id="@+id/textChanger"
      android:layout_margin="20dp"
      android:textAlignment="center"
      android:text="Initial text"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />
</LinearLayout>

Trong đoạn mã trên, chúng ta đã sử dụng chế độ xem Hình ảnh và chế độ xem văn bản. Khi người dùng nhấp vào chế độ xem Văn bản, Hình ảnh sẽ xoay theo góc 20 độ.

Bước 3 - Thêm mã sau vào src / MainActivity.java

package com.example.andy.myapplication;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
   int view = R.layout.activity_main;
   TextView textChanger;
   ImageView imageView;
   @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(view);
      textChanger = findViewById(R.id.textChanger);
      imageView=findViewById(R.id.imageView);
      textChanger.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            roateImage(imageView);
         }
      });
   }
   private void roateImage(ImageView imageView) {
      Matrix matrix = new Matrix();
      imageView.setScaleType(ImageView.ScaleType.MATRIX); //required
      matrix.postRotate((float) 20, imageView.getDrawable().getBounds().width()/2,    imageView.getDrawable().getBounds().height()/2);
      imageView.setImageMatrix(matrix);
   }
}

Trong đoạn mã trên, chúng ta phải sử dụng roateImage () và truyền imageview như hình dưới đây -

Matrix matrix = new Matrix();
imageView.setScaleType(ImageView.ScaleType.MATRIX); //required
matrix.postRotate((float) 20, imageView.getDrawable().getBounds().width()/2, imageView.getDrawable().getBounds().height()/2);
imageView.setImageMatrix(matrix);

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ studio android, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Run từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Làm thế nào để xoay hình ảnh trong chế độ xem hình ảnh theo một góc trong Android?

Trong đoạn mã trên, nó là một màn hình mặc định. Khi người dùng nhấp vào chế độ xem văn bản "văn bản ban đầu", nó sẽ xoay 20 độ như hình dưới đây -

Làm thế nào để xoay hình ảnh trong chế độ xem hình ảnh theo một góc trong Android?