刚开始做安卓,写了一个简陋的图片缩放程序。费了一点时间,看来还是有必要记录下来。

布局代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:layout_gravity="clip_vertical" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:gravity="bottom|center_horizontal"        android:orientation="horizontal" >        <Button            android:id="@+id/bsx"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="缩小" />        <Button            android:id="@+id/bfd"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="放大" />    </LinearLayout>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="vertical" >        <ImageView            android:id="@+id/img"            android:layout_width="wrap_content"            android:layout_height="wrap_content" >        </ImageView>    </LinearLayout></FrameLayout>

java代码:

package com.example.myscalepic;import android.os.Bundle;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Matrix;import android.util.DisplayMetrics;import android.util.Log;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {    ImageView imgView;    Bitmap bmp;    int screenWidth; //屏幕宽度    int screenHeight;//屏幕高度    float scaleWidth = 1f; //缩放倍率    float scaleHeight = 1f;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bmp = BitmapFactory.decodeResource(getResources(), R.drawable.feng); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels - 80; imgView = (ImageView) this.findViewById(R.id.img); Button b1 = (Button) this.findViewById(R.id.bsx); Button b2 = (Button) this.findViewById(R.id.bfd); //设置图片 imgView.setImageBitmap(bmp); b1.setOnClickListener(this); b2.setOnClickListener(this); }
@Override
public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onClick(View v) { if (v.getId() == R.id.bfd) {//点击放大按钮 scaleWidth *= 1.2; scaleHeight *= 1.2; } else { //点击缩小按钮 scaleWidth *= 0.8; scaleHeight *= 0.8; } //设置新的图片 imgView.setImageBitmap(scaleToFit()); } /** * 缩放函数 * @return */ private Bitmap scaleToFit() { int width = bmp.getWidth(); int height = bmp.getHeight(); Matrix ma = new Matrix(); ma.postScale(scaleWidth, scaleHeight); Bitmap tewmpmap; if (width * scaleWidth > screenWidth|| height * scaleHeight > screenHeight) { scaleWidth=1f; scaleHeight=1f; return bmp; } else { //得到图片缩放后的副本 tewmpmap = Bitmap.createBitmap(bmp, 0, 0, width, height, ma, true); return tewmpmap; } }}

更多相关文章

  1. Android基础小技术点:Android ListView设置背景图片及分割线、周
  2. Android -- 加载大图片的方法
  3. android WindowManager可拖动悬浮按钮
  4. Android WebView 支持H5图片上传
  5. Android上传图片(PHP服务器)
  6. android沉浸式状态栏底部背景用图片代替
  7. 浅入浅出Android(015):使用ImageView显示网络图片
  8. Android alertdialog的自定义添加按钮和添加点击
  9. IKNinePhotoView 是一个开源的 Android 九宫格控件,可以自适应宽

随机推荐

  1. 最完整Android(安卓)Studio插件整理 (转)
  2. AIDL使用和Binder机制详解
  3. android 利用java反射调用系统类的隐藏方
  4. Android使用getIdentifier()方法根据资源
  5. 让你的代码减少三倍!使用kotlin开发Androi
  6. Android(安卓)电量分析之Battery Histori
  7. Android(安卓)6.0运行时权限 (危险权限)
  8. android自定义大小对话框
  9. Android原生(Native)C开发之六:libpng移植
  10. Android+本地记事本(上)------实现登陆注