欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android中imageView图片放大缩小及旋转功能示例代码

时间:2021-05-07 10:50:48|栏目:Android代码|点击:

一、简介

二、方法

1)设置图片放大缩小效果

第一步:将<ImageView>标签中的android:scaleType设置为"fitCenter"

android:scaleType="fitCenter"

第二步:获取屏幕的宽度

DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
dm.widthPixels

第三步:设置seekBar的最大progree值为屏幕宽度

sb_one.setMax(dm.widthPixels);

第四步:设置imageview的布局参数,也就是宽和高,也就是画布的宽高

int width=progress;
int height=progress*3/4;
iv_pic.setLayoutParams(new LinearLayout.LayoutParams(width, height));

2)设置图片旋转方法

第一步:给matrix设置角度,用于新的bitmap

private Matrix matrix;
matrix.setRotate((int)(progress*3.60));

第二步:获取bitmap资源

BitmapDrawable bitmapDrawable=(BitmapDrawable)(getResources().getDrawable(R.drawable.image1));
Bitmap bitmap=bitmapDrawable.getBitmap();

第三步:重建bitmap用于显示

Bitmap newBitmap=bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),matrix,false);

第四步:给imageview设置新的bitmap

iv_pic.setImageBitmap(newBitmap);

三、代码实例

效果图:

设置大小和设置旋转的效果图

代码:

fry.Activity02

package fry;
import com.example.iamgeViewDemo1.R;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class Activity02 extends Activity implements OnSeekBarChangeListener{
  private ImageView iv_pic;
  private SeekBar sb_one;
  private SeekBar sb_two;
  private Matrix matrix;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    setTitle("imageView实现图片缩放和旋转");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity02);
    iv_pic=(ImageView) findViewById(R.id.iv_pic);
    sb_one=(SeekBar) findViewById(R.id.sb_one);
    sb_two=(SeekBar) findViewById(R.id.sb_two);
    //设置SeekBar的progress值改变监听事件
    sb_one.setOnSeekBarChangeListener(this);
    sb_two.setOnSeekBarChangeListener(this);
    matrix=new Matrix();
//    1)设置图片放大缩小效果
//
//    第一步:将<ImageView>标签中的android:scaleType设置为"fitCenter"
//
//    第二步:获取屏幕的宽度
//
//    第三步:设置seekBar的最大progree值为屏幕宽度
//
//    第四步:设置imageview的布局参数,也就是宽和高,也就是画布的宽高
    //设置图片放大缩小效果
    //第一步:获取屏幕的宽度
    DisplayMetrics dm=new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    //第二步:设置seekBar的最大progree值为屏幕宽度
    sb_one.setMax(dm.widthPixels);
  }
  @Override
  public void onProgressChanged(SeekBar seekBar, int progress,
      boolean fromUser) {
    // TODO Auto-generated method stub
    switch (seekBar.getId()) {
    case R.id.sb_one://放大或缩小
      int width=progress;
      int height=progress*3/4;
      //第三步:设置imageview的布局参数,也就是宽和高,也就是画布的宽高
      iv_pic.setLayoutParams(new LinearLayout.LayoutParams(width, height));
      break;
    case R.id.sb_two://旋转
      //设置旋转度数
      //设置图片旋转方法
      //第一步:给matrix设置角度,用于新的bitmap
      matrix.setRotate((int)(progress*3.60));
      //第二步:获取bitmap资源
      BitmapDrawable bitmapDrawable=(BitmapDrawable)(getResources().getDrawable(R.drawable.image1));
      Bitmap bitmap=bitmapDrawable.getBitmap();
      //第三步:重建bitmap用于显示
      Bitmap newBitmap=bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),matrix,false);
      //第四步:给imageview设置新的bitmap
      iv_pic.setImageBitmap(newBitmap);
      break;
    default:
      break;
    }
  }
  @Override
  public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
  }
  @Override
  public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
  }
}

activity02.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
  <ImageView 
    android:id="@+id/iv_pic"
    android:layout_width="match_parent"
    android:layout_height="300dip"
    android:background="@android:color/black"
    android:scaleType="fitCenter"
    android:src="@drawable/image1"
    />
  <!-- 设置图片的显示方式:把图片按比例扩大/缩小到view的宽度,居中显示 -->
  <SeekBar
    android:id="@+id/sb_one"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:progress="100"
    />
  <TextView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="拖动来缩放图片"
    />
  <SeekBar 
    android:id="@+id/sb_two"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
  <TextView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="拖动来旋转图片"
    />
</LinearLayout>

四、收获

 1、设置图像居中显示

android:scaleType="fitCenter"

总结

上一篇:Android实现通知栏透明的方法

栏    目:Android代码

下一篇:解决Android webview设置cookie和cookie丢失的问题

本文标题:Android中imageView图片放大缩小及旋转功能示例代码

本文地址:http://www.codeinn.net/misctech/116755.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有