Android Imageview图片旋转和大小变化

Android Imageview图片旋转和大小变化

先看效果图:

Android Imageview图片旋转和大小变化_第1张图片

直接上代码吧

activity.java


import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {

    private ImageView iv_route_image;
    private TextView tv_size;
    private TextView tv_route;
    private SeekBar sb_change_size;
    private SeekBar sb_change_route;
    private int minWidth = 300;
    private int routeAngle;
    private Matrix matrix = new Matrix();

    private static final int ROUTE_IMAGE_MSG = 1;

    /**
     * 使用handle似乎旋转更流畅点(博主希望你们有更好的建议,是旋转更流畅,欢迎评论)
     */
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case ROUTE_IMAGE_MSG:
                {
                    tv_route.setText("角度:    "+routeAngle);
                    //先获取目标图片的参数信息
                    Bitmap bitmap = ((BitmapDrawable)(getResources().getDrawable(R.drawable.back))).getBitmap();
                    matrix.setRotate(routeAngle);//设置反转角度
                    bitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
                    iv_route_image.setImageBitmap(bitmap);
                }
                break;
                default:
                    break;
            }

        }
    };


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

    private void initData() {
        //获取屏幕参数
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        sb_change_size.setMax(displayMetrics.widthPixels-minWidth);//屏幕宽度-最小值

    }


    private void initView() {

          iv_route_image = findViewById(R.id.iv_route_image);
          tv_size= findViewById(R.id.tv_size);
          tv_route= findViewById(R.id.tv_route);

          sb_change_size= findViewById(R.id.sb_change_size);
          sb_change_size.setOnSeekBarChangeListener(this);
          sb_change_route= findViewById(R.id.sb_change_route);
          sb_change_route.setOnSeekBarChangeListener(this);

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        switch (seekBar.getId()){
            case R.id.sb_change_size:
            {
                tv_size.setText("大小:    "+i);

                int newWidth = i+minWidth;
                int newHeight = (int)newWidth*3/4;//根据image view的大小就是一个3/4的比例,同时新的大小也应该满足
                iv_route_image.setLayoutParams(new LinearLayout.LayoutParams(newWidth,newHeight));//设置image view布局大小

            }
            break;
            case R.id.sb_change_route:
            {
                routeAngle = i;
                Message msg = new Message();
                msg.what = ROUTE_IMAGE_MSG;
                handler.sendMessage(msg);
            }
            break;
            default:
                break;
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}

布局文件




    
    

   

       
       


       
       


       
       

       
       

   


你可能感兴趣的:(Android)