展会信息港展会大全

Android Matrix放大缩小图片
来源:互联网   发布日期:2015-10-02 16:11:11   浏览:2527次  

导读:package androidstudy.matrix; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.os.Bundle......

package androidstudy.matrix;

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.widget.Button;

import android.widget.ImageView;

import android.widget.RelativeLayout;

public class maxtrix extends Activity {

private ImageView mImageView;

private Button mButton1;

private Button mButton2;

private RelativeLayout layout1;

private Bitmap bmp;

private int id = 0;

private int displayWidth;

private int displayHeight;

private float scaleWidth = 1;

private float scaleHeight = 1;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

// 取得屏幕分辨率大小

DisplayMetrics dm = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(dm);

displayWidth = dm.widthPixels;

displayHeight = dm.heightPixels - 80;

bmp = BitmapFactory.decodeResource(getResources(), R.drawable.q2);

mImageView = (ImageView) findViewById(R.id.imageView1);

layout1 = (RelativeLayout) findViewById(R.id.layout1);

mButton1 = (Button) findViewById(R.id.button1);

mButton2 = (Button) findViewById(R.id.button2);

mButton1.setOnClickListener(new Button.OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

small();

}

});

mButton2.setOnClickListener(new Button.OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

big();

}

});

}

public void small() {

int bmpWidth = bmp.getWidth();

int bmpHeight = bmp.getHeight();

double scale = 0.8;

scaleWidth = (float) (scaleWidth * scale);

scaleHeight = (float) (scaleHeight * scale);

Matrix matrix = new Matrix();

matrix.postScale(scaleWidth, scaleHeight);

Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,

matrix, true);

if (id == 0) {// 第一次就删除原来的Imageview

layout1.removeView(mImageView);

} else {// 不是第一次删除上一次产生的ImageView

layout1.removeView((ImageView) findViewById(id));

}

id++;

ImageView imageView = new ImageView(maxtrix.this);

imageView.setId(id);

imageView.setImageBitmap(resizeBmp);

layout1.addView(imageView);

setContentView(layout1);

// 图片最大的时候。放大按钮disable,缩小的时候设为enable

mButton2.setEnabled(true);

}

public void big() {

int bmpWidth = bmp.getWidth();

int bmpHeight = bmp.getHeight();

double scale = 1.25;

scaleWidth = (float) (scaleWidth * scale);

scaleHeight = (float) (scaleHeight * scale);

Matrix matrix = new Matrix();

matrix.postScale(scaleWidth, scaleHeight);

Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,

matrix, true);

if (id == 0) {// 第一次就删除原来的Imageview

layout1.removeView(mImageView);

} else {// 不是第一次删除上一次产生的ImageView

layout1.removeView((ImageView) findViewById(id));

}

id++;

ImageView imageView = new ImageView(maxtrix.this);

imageView.setId(id);

imageView.setImageBitmap(resizeBmp);

layout1.addView(imageView);

setContentView(layout1);

// 如果在放大图片就超出屏幕,就把Button disable

if (scaleWidth * scale * bmpWidth > displayWidth

|| scaleHeight * scale * bmpHeight > displayHeight)

mButton2.setEnabled(false);

}

}

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/layout1"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

<ImageView

android:id="@+id/imageView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

<Button

android:id="@+id/button1"

android:layout_alignParentBottom="true"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="Button"

/>

<Button

android:id="@+id/button2"

android:layout_toRightOf="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignTop="@+id/button1"

android:layout_alignBottom="@+id/button1"

android:text="Button"

/>

</RelativeLayout>

赞助本站

人工智能实验室

相关热词: Matrix 放大 缩小 图片

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港