package funo.com.test;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ChangePicture extends Activity {

private ImageView mImageView;
private Button btn1,btn2,btn3;
private LinearLayout layout1;
private Bitmap bmp;
private int id=0;
private int displayWidth,displayHeight;
private float scaleWidth=1,scaleHeight=1;
private int degree = 30;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.picture);
//取得屏幕分辨率
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
displayWidth=dm.widthPixels;
displayHeight=dm.heightPixels-80;

System.out.println(displayWidth +","+displayHeight);

bmp=BitmapFactory.decodeResource(this.getResources(),R.drawable.pic);
layout1=(LinearLayout)findViewById(R.id.layout1);
mImageView=(ImageView)findViewById(R.id.myImageView);
btn1=(Button)findViewById(R.id.myButton1);
btn1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
small();
}
});
btn2=(Button)findViewById(R.id.myButton2);
btn2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
big();
}
});
btn3 = (Button)findViewById(R.id.myButton3);
btn3.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
circle();

}
});
}
private void small(){
//获得Bitmap的高和宽
int bmpWidth=bmp.getWidth();
int bmpHeight=bmp.getHeight();
//设置缩小比例
double scale=0.8;
//计算出这次要缩小的比例
scaleWidth=(float)(scaleWidth*scale);
scaleHeight=(float)(scaleHeight*scale);
//产生resize后的Bitmap对象
Matrix matrix=new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp=Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
if(id==0){
layout1.removeView(mImageView);
}
else{
layout1.removeView((ImageView)findViewById(id));

}
id++;
ImageView imageView=new ImageView(this);
imageView.setId(id);
imageView.setImageBitmap(resizeBmp);
layout1.addView(imageView);
setContentView(layout1);
btn2.setEnabled(true);
}
private void big(){
//获得Bitmap的高和宽
int bmpWidth=bmp.getWidth();
int bmpHeight=bmp.getHeight();
//设置缩小比例
double scale=1.25;
//计算出这次要缩小的比例
scaleWidth=(float)(scaleWidth*scale);
scaleHeight=(float)(scaleHeight*scale);
//产生resize后的Bitmap对象
Matrix matrix=new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp=Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
if(id==0){
layout1.removeView(mImageView);
}
else{
layout1.removeView((ImageView)findViewById(id));

}
id++;
ImageView imageView=new ImageView(this);
imageView.setId(id);
imageView.setImageBitmap(resizeBmp);
layout1.addView(imageView);
setContentView(layout1);
if(scaleWidth*scale*bmpWidth>displayWidth||scaleHeight*scale*scaleHeight>displayHeight){
btn2.setEnabled(false);
}
}
//旋转
private void circle() {
//位图的旋转
//获得Bitmap的高和宽
int bmpWidth=bmp.getWidth();
int bmpHeight=bmp.getHeight();

//产生resize后的Bitmap对象
Matrix matrix=new Matrix();
matrix.postRotate(degree);
degree += 30;
Bitmap resizeBmp=Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
// int id = 0;
if(id==0){
layout1.removeView(mImageView);
}
else{
layout1.removeView((ImageView)findViewById(id));

}

id++;
ImageView imageView=new ImageView(this);
imageView.setId(id);
imageView.setImageBitmap(resizeBmp);
layout1.addView(imageView);
setContentView(layout1);

}

}




//xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"

>
<Button
android:id="@+id/myButton1"
android:layout_width="90px"
android:layout_height="60px"
android:text="缩小"
android:textSize="18sp"
>
</Button>
<Button
android:id="@+id/myButton2"
android:layout_width="90px"
android:layout_height="60px"
android:text="放大"
android:textSize="18sp"
>
</Button>
<Button
android:id="@+id/myButton3"
android:layout_width="90px"
android:layout_height="60px"
android:text="旋转"
android:textSize="18sp"



/>



</LinearLayout>
<ImageView
android:id="@+id/myImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/pic"
android:layout_x="0px"
android:layout_y="0px"
>
</ImageView>


</LinearLayout>

更多相关文章

  1. Android实现动画持续旋转
  2. Android实现图片反转、翻转、旋转、放大和缩小
  3. android图片放大 缩小 旋转
  4. Android(安卓)通过CANVAS旋转 绘制文字 竖直方向DEMO
  5. android 横屏竖屏设置
  6. 图片的ScaleType详解 ImageView的属性android:scaleType,
  7. TabHost和android:layout_height="0.0dip"以及android:layout_we
  8. TabHost和android:layout_height="0.0dip"以及android:layout_we
  9. Android(安卓)Studio属性学习(三)——四种基本动画属性

随机推荐

  1. android添加广告之--admob
  2. Android中的背景音频与MediaSessionCompa
  3. Android(安卓)ScreenShot 屏幕截图
  4. Android(安卓)Framework 基础
  5. Android(安卓)横向选择器(HorizontalPick
  6. android 下载安装并打开apk
  7. Android(安卓)SharedPreferences一般的读
  8. android美化对话框
  9. Android(安卓)TextView支持的HTML标签
  10. 12 Android(安卓)AsyncHttpClient 的简单