在一些软件经常用到图片的放大或者缩小的功能,使图片达到较为理想的视觉效果。在本程序中将实现这项功能,主要是对ImageView这个控件的应用。
效果图如下:
点击放大后:
实现这项功能并不是很难,首先开始布局文件,定义一个ImageView控件和两个按钮控件.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="horizontal"
>
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="放大"
/>
<Button
android:id="@+id/low"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩小"
/>
</LinearLayout>
</LinearLayout>
布局文件很简单,一个LinearLayout嵌套一个LinearLayout。
然后完成主功能代码。
package com.example.imageview;
import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Matrix.ScaleToFit;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
private ImageView imageView;
private Button btn_add;
private Button btn_low;
private Bitmap bmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
imageView = (ImageView) this.findViewById(R.id.image);
imageView.setImageBitmap(bmp);
btn_add = (Button) this.findViewById(R.id.add);
btn_add.setOnClickListener(new OnClickListenerAdd());
btn_low = (Button) this.findViewById(R.id.low);
btn_low.setOnClickListener(new OnClickListenerLow());
}
//放大
private final class OnClickListenerAdd implements OnClickListener{
@Override
public void onClick(View v) {
imageView.setImageBitmap(ScaleToFit(bmp,1.2f));//放大
bmp = ScaleToFit(bmp, 1.2f);
}
}
//缩小
private final class OnClickListenerLow implements OnClickListener{
@Override
public void onClick(View v) {
imageView.setImageBitmap(ScaleToFit(bmp,0.8f));//放大
bmp = ScaleToFit(bmp, 0.8f);
}
}
public Bitmap ScaleToFit(Bitmap bmp, float scale) {
int width = bmp.getWidth();
int height = bmp.getHeight();
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
Bitmap bmResult = Bitmap.createBitmap(bmp,0,0,width,height,matrix,true);
return bmResult;
}
}
为两个按钮按钮监听事件,完成放大缩小功能主要通过函数postScale(float f,float f);实现。
介绍个函数:
- public static Bitmap createBitmap (Bitmap source,
int x, int y, int width, int height, Matrix m, boolean filter)
从原始位图剪切图像,这是一种高级的方式。可以用Matrix(矩阵)来实现旋转等高级方式截图
参数说明:
Bitmap source:要从中截图的原始位图
int x:起始x坐标
int y:起始y坐标
int width:要截的图的宽度
int height:要截的图的宽度
Bitmap.Config config:一个枚举类型的配置,可以定义截到的新位图的质量
返回值:返回一个剪切好的Bitmap
实现图片的放大与缩小,还有种方法就是在res/anim文件下新建一个xml文件,在里面可以自己定义各种各样的动画。alpha,scale,rotate,translate.自己可以查阅文档。在实际项目中,Bitmap图片非常耗时耗内存。建议少使用。
本文介绍了在Android中如何实现图片的放大与缩小功能,主要通过应用ImageView控件来达到理想视觉效果。程序中详细讲解了布局文件的设置、关键代码实现,并提到了一个用于剪切图像的Bitmap创建函数。此外,还提及了使用res/anim下的xml文件定义动画来实现图片变换,但建议避免过多使用Bitmap以减少性能消耗。
821

被折叠的 条评论
为什么被折叠?



