AI智能
改变未来

Android实现图片缩放和旋转

xml里代码

<?xml version=\”1.0\” encoding=\”utf-8\”?>

<ImageViewandroid:id=\"@+id/pic\"android:l![在这里插入图片描述](Android开发/https://aiznh.com/wp-content/uploads/2021/06/20210606122516-60bcbeacf323b.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTc3Njc2Mw==,size_16,color_FFFFFF,t_70)ayout_width=\"match_parent\"android:layout_height=\"300dip\"android:scaleType=\"fitCenter\"android:src=\"@drawable/pic1\" /><TextViewandroid:id=\"@+id/textview1\"android:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:text=\"拖动来缩放图片:\"android:textSize=\"20dp\" /><SeekBarandroid:id=\"@+id/sb_one\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"android:progress=\"100\" /><TextViewandroid:id=\"@+id/textview2\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"android:text=\"拖到来旋转图片:\"android:textSize=\"25dp\" /><SeekBarandroid:id=\"@+id/sb_two\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\" />

mainApp类代码

package com.example.mydialog;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
private ImageView pic;
private TextView textview1;
private TextView textview2;
private SeekBar sb_one;
private SeekBar sb_two;
private Matrix matrix;
private int width ;
private int height;

@Overrideprotected void onCreate(Bundle savedInstanceState) {setTitle(\"ImageView实现图片缩放和旋转\");super.onCreate(savedInstanceState);setContentView(R.layout.picture);pic = (ImageView) findViewById(R.id.pic);textview1 = (TextView) findViewById(R.id.textview1);textview2 = (TextView) findViewById(R.id.textview2);sb_one = (SeekBar) findViewById(R.id.sb_one);sb_two = (SeekBar) findViewById(R.id.sb_two);sb_one.setOnSeekBarChangeListener(this);sb_two.setOnSeekBarChangeListener(this);matrix = new Matrix();DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);sb_one.setMax(dm.widthPixels);}@Overridepublic void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {//获取图片大小BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeResource(getResources(), R.drawable.pic1, options);options.inSampleSize = 1;options.inJustDecodeBounds = false;width = options.outWidth;height = options.outHeight;if (seekBar.getId() == R.id.sb_one) {int  newwidth = progress;int  newheight = progress * height / width;pic.setLayoutParams(new LinearLayout.LayoutParams(newwidth, newheight));textview1.setText(\"图像宽度:\" + newwidth + \"      图像高度:\" + newheight);} else if (seekBar.getId() == R.id.sb_two) {matrix.setRotate(progress);BitmapDrawable bitmapDrawable = (BitmapDrawable) (getResources().getDrawable(R.drawable.pic1));Bitmap bitmap = bitmapDrawable.getBitmap();Bitmap newBitmap = bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);pic.setImageBitmap(newBitmap);textview2.setText(progress+\"度\");}}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {}

}

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » Android实现图片缩放和旋转