Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

จะหมุนภาพในมุมมองภาพตามมุมใน Android ได้อย่างไร?


ตัวอย่างนี้สาธิตวิธีการหมุนภาพในมุมมองภาพตามมุม

ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่

ขั้นตอนที่ 2 − เพิ่มรหัสต่อไปนี้ใน 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>

ในโค้ดด้านบน เราได้ใช้มุมมองภาพและมุมมองข้อความ เมื่อผู้ใช้คลิกที่มุมมองข้อความ รูปภาพจะหมุนเป็นมุม 20 องศา

ขั้นตอนที่ 3 − เพิ่มรหัสต่อไปนี้ใน 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);
   }
}

ในโค้ดด้านบนนี้ เราต้องใช้ roateImage() และส่งผ่าน 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);

มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก แล้วตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ -

จะหมุนภาพในมุมมองภาพตามมุมใน Android ได้อย่างไร?

ในโค้ดด้านบนจะเป็นหน้าจอเริ่มต้น เมื่อผู้ใช้คลิกที่มุมมองข้อความ "ข้อความเริ่มต้น" มันจะหมุน 20 องศาดังที่แสดงด้านล่าง -

จะหมุนภาพในมุมมองภาพตามมุมใน Android ได้อย่างไร?