码迷,mamicode.com
首页 > 移动开发 > 详细

Android -- ImageView(控制图片的大小以及旋转的角度)

时间:2015-03-10 10:12:46      阅读:559      评论:0      收藏:0      [点我收藏+]

标签:

1.  技术分享

 

2.   实现代码

package com.example.myimageview3;

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.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnSeekBarChangeListener {

	private int minWidth = 80;

	/* ImageView 图片 */
	private ImageView imageView;

	/* 显示图片的信息以及角度 */
	private TextView textView1 , textView2;

	/* 大小和角度拖动条 */
	private SeekBar seekBar1 , seekBar2;

	/* 角度变化的矩阵 */
	private Matrix matrix = new Matrix ( );

	@ Override
	protected void onCreate ( Bundle savedInstanceState ) {

		super.onCreate ( savedInstanceState );
		setContentView ( R.layout.activity_main );

		//  找到组件
		imageView = ( ImageView ) this.findViewById ( R.id.imageview1 );
		textView1 = ( TextView ) this.findViewById ( R.id.textview1 );
		textView2 = ( TextView ) this.findViewById ( R.id.textview2 );
		seekBar1 = ( SeekBar ) this.findViewById ( R.id.seekbar1 );
		seekBar2 = ( SeekBar ) this.findViewById ( R.id.seekbar2 );

		//  设置监听事件
		seekBar1.setOnSeekBarChangeListener ( this );
		seekBar2.setOnSeekBarChangeListener ( this );

		// 获取当前手机显示的通用信息,并将当前窗口的一些信息放在DisplayMetrics 中
		DisplayMetrics dm = new DisplayMetrics ( );
		getWindowManager ( ).getDefaultDisplay ( )
		                              .getMetrics ( dm );
		
		//  设置seekBar1的最大值为获取屏幕的像素-80
		seekBar1.setMax ( dm.widthPixels - minWidth );
	}

	/* Seekbar 监听事件 */
	@ Override
	public void onProgressChanged ( SeekBar seekBar , int progress ,
	                              boolean fromUser ) {

		//  SeekBar1 处理事件
		// TODO Auto-generated method stub
		if (seekBar.getId ( ) == R.id.seekbar1) {
			
			//  图片新的宽度
			int newWidth = progress + minWidth;
			// 图片新的高度为宽度的3/4
			int newHeight = ( int ) ( newWidth * 3 / 4 );//按照原图片进行缩放的功能
			// 设置图片的高度和宽度
			imageView.setLayoutParams ( new LinearLayout.LayoutParams (
			                              newWidth ,
			                              newHeight ) );
			// 设置TextView1显示的信息
			textView1.setText ( "图像宽度"
			                              + newWidth
			                              + "图像高度"
			                              + newHeight );
		}

		//  SeekBar2 处理事件
		else if (seekBar.getId ( ) == R.id.seekbar2) {
			
			
			Bitmap bitmap = ( ( BitmapDrawable ) ( getResources ( )
			                              .getDrawable ( R.drawable.dog ) ) )
			                              .getBitmap ( );
			
			// 设置图片旋转的角度
			matrix.setRotate ( progress );
			bitmap = Bitmap.createBitmap (
			                              bitmap ,
			                              0 ,
			                              0 ,
			                              bitmap.getWidth ( ) ,
			                              bitmap.getHeight ( ) ,
			                              matrix ,
			                              true );
			imageView.setImageBitmap ( bitmap );
			textView2.setText ( progress + "度" );
		}
	}

	@ Override
	public void onStartTrackingTouch ( SeekBar seekBar ) {

		// TODO Auto-generated method stub

		Toast.makeText ( MainActivity.this , "开始拖动" ,
		                              Toast.LENGTH_SHORT )
		                              .show ( );
	}

	@ Override
	public void onStopTrackingTouch ( SeekBar seekBar ) {

		// TODO Auto-generated method stub
		Toast.makeText ( MainActivity.this , "停止拖动" ,
		                              Toast.LENGTH_SHORT )
		                              .show ( );
	}

}

 

Android -- ImageView(控制图片的大小以及旋转的角度)

标签:

原文地址:http://www.cnblogs.com/SM-t/p/4325173.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!