zoukankan      html  css  js  c++  java
  • 刚开始安卓,记录一个刚做的图片缩放程序

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

    布局代码:

    <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; } } }
  • 相关阅读:
    Everything
    图片在下载过程中由模糊逐渐变清晰的技巧
    Windows下Critical Section、Event、Mutex、Semaphores区别
    二维Cookie操作(JS和ASP)
    ASP.net利用urlMappings重写URL路径(URL映射)
    GHOST (以硬盘为单位和以分区分区为单位还原)>个人观点:)
    简单计算器
    ASP操作COOKIE
    擦亮自己的眼睛去看SQLServer之谈谈锁机制
    SQL SERVER2008跟踪标志
  • 原文地址:https://www.cnblogs.com/kyyblabla/p/3130152.html
Copyright © 2011-2022 走看看