刚开始安卓,记录一个刚做的图片缩放程序
16lz
2022-01-09
刚开始做安卓,写了一个简陋的图片缩放程序。费了一点时间,看来还是有必要记录下来。
布局代码:
<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; } }}
更多相关文章
- Android上传图片(PHP服务器)
- android 显示网络图片
- android Selector的用法
- Android(安卓)WebView 支持H5图片上传
- Android(安卓)-- 加载大图片的方法
- Android基础小技术点:Android(安卓)ListView设置背景图片及分割线
- 浅入浅出Android(015):使用ImageView显示网络图片
- android沉浸式状态栏底部背景用图片代替
- android selector下的设置背景属性值