一批大小不同的图片 经过处理变成全部统一指定大小的图片

第一次在CSDN写博客,勉励自己要多写多努力!

前两天一同学问我图片大小的处理,原话是这样“一批大小不同的图片 经过处理变成全部统一指定大小的图片”。
之后我开始着手处理这个问题,因为我最近开发的Android项目都是企业级产品很少涉及到图片,所以对Bitmap处理比较生疏。

代码量非常的少,所以废话不多说,请看下面的代码,如大家发现问题。请及时通知我修改。谢谢!

先看下效果
示例

我先上网查找了一些资料,关于bitmap的一些处理图片的方式。
布局文件fragment_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.hczxhy.propanim.MainActivity$PlaceholderFragment" >

    <ImageView
        android:id="@+id/iv_showIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:layout_margin="15dp"
        android:src="@drawable/angelababy_icon2" />

    <TextView
        android:id="@+id/tv_hellword"
        style="@android:style/Widget.TextView.SpinnerItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="57dp"
        android:text="@string/hello_world"
        android:textSize="24sp"
        android:visibility="gone" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@+id/button1"
        android:text="指定大小计算"
        android:textSize="15sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText2"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="28dp"
        android:text="按比例计算"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText1"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignParentLeft="true"
        android:text="宽"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/iv_showIcon"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:hint="请输入图片的宽"
        android:inputType="number"
        android:maxLength="3"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:ems="10"
        android:hint="请输入图片的高"
        android:inputType="number"
        android:maxLength="3"
        android:textSize="20sp" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText2"
        android:layout_alignBottom="@+id/editText2"
        android:layout_alignLeft="@+id/textView1"
        android:text="高"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="20sp" />

</RelativeLayout>

代码文件MainActivity.java

package com.tzz.ist;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.app.Fragment;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
/**
 * 比较简单的图片处理例子
 * @author Tzz
 * @date 2015-04-27 18:25:26
 */
public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment implements OnClickListener {

        public PlaceholderFragment() {
        }

        /**
         * 计算图片比例
         * @param options
         * @param reqWidth
         * @param reqHeight
         * @return
         */
        public static int calculateInSampleSize(BitmapFactory.Options options,
                int reqWidth, int reqHeight) {
            // 源图片的高度和宽度
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;
            if (height > reqHeight || width > reqWidth) {
                // 计算出实际宽高和目标宽高的比率
                final int heightRatio = Math.round((float) height / (float) reqHeight);
                final int widthRatio = Math.round((float) width / (float) reqWidth);
                // 选择宽和高中最小的比率作为inSampleSize的值,这样可以保证最终图片的宽和高
                // 一定都会大于等于目标的宽和高。
                inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
            }
            return inSampleSize;
        }

        /**
         * 根据给入的图片大小获取最好的图片比例图
         * @param res
         * @param resId
         * @param reqWidth
         * @param reqHeight
         * @return
         */
        public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                int reqWidth, int reqHeight) {
            // 第一次解析将inJustDecodeBounds设置为true,来获取图片大小
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeResource(res, resId, options);
            // 调用上面定义的方法计算inSampleSize值
            options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
            // 使用获取到的inSampleSize值再次解析图片
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeResource(res, resId, options);
        }


        /**
         *  放到或缩小图片处理图片 
         * @param bm 所要转换的bitmap
         * @param newWidth新的宽
         * @param newHeight新的高  
         * @return 指定宽高的bitmap
         */
         public static Bitmap zoomImg(Bitmap bm, int newWidth ,int newHeight){  
            // 获得图片的宽高  
            int width = bm.getWidth();  
            int height = bm.getHeight();  
            // 计算缩放比例  
            float scaleWidth = ((float) newWidth) / width;  
            float scaleHeight = ((float) newHeight) / height;  
            // 取得想要缩放的matrix参数  
            Matrix matrix = new Matrix();  
            matrix.postScale(scaleWidth, scaleHeight);  
            // 得到新的图片  
            Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);  
            return newbm;  
        } 


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            //处理后的图片由该图片组件显示
            iv_ShowIcon = (ImageView) rootView.findViewById(R.id.iv_showIcon);

            View button1 = rootView.findViewById(R.id.button1);//指定大小计算
            View button2 = rootView.findViewById(R.id.button2);//按比例计算
            editText1 = (EditText) rootView.findViewById(R.id.editText1);
            editText2 = (EditText) rootView.findViewById(R.id.editText2);
            calculateOnClickListeter clickListeter = new calculateOnClickListeter();
            button1.setOnClickListener(clickListeter);
            button2.setOnClickListener(clickListeter);

            return rootView;
        }

        class calculateOnClickListeter implements OnClickListener {

            @Override
            public void onClick(View v) {
                Bitmap btp = null;
                int width = 100;//默认100
                int height = 100;//默认100
                String wdStr = editText1.getText().toString();
                String htStr = editText2.getText().toString();
                //如果宽不为空设置宽的大小
                if(!TextUtils.isEmpty(wdStr)) {
                    width = Integer.parseInt(wdStr);
                }
                //如果宽不为空设置宽的大小
                if(!TextUtils.isEmpty(htStr)) {
                    height = Integer.parseInt(htStr);
                }
                if(v.getId() == R.id.button1) {
                     btp = decodeSampledBitmapFromResource(
                                getResources(), R.drawable.angelababy_icon2, width, height);
                } else {
                    btp = BitmapFactory.decodeResource(getResources(), R.drawable.angelababy_icon2);
                    btp = zoomImg(btp, width, height);
                }
                iv_ShowIcon.setImageBitmap(btp);
                //记得回收bitmap,这是一个好习惯。
                if(btp != null && btp.isRecycled()) {
                    btp.recycle();
                    btp = null;
                }
            }

        }


        //============================以下内容为其他测试====================================//

        ObjectAnimator alpha;//透明渐变
        ObjectAnimator rotation;//旋转
        ObjectAnimator translationX;//向左移动
        ObjectAnimator scaleY;//缩小放大
        private AnimatorSet animatorSet;
        private ImageView iv_ShowIcon;
        private EditText editText1;
        private EditText editText2;
        @Override
        public void onClick(View v) {
            if(alpha == null) {
                float translationX2 = v.getTranslationX();
                alpha = ObjectAnimator.ofFloat(v, "alpha", 1f,0f,1f);
                rotation = ObjectAnimator.ofFloat(v, "rotation", 0f,360f);
                translationX = ObjectAnimator.ofFloat(v, "translationX", translationX2,-500f,translationX2);
                scaleY = ObjectAnimator.ofFloat(v, "scaleY", 1f,3f,1f);
                alpha.setDuration(3000);
                rotation.setDuration(3000);
                translationX.setDuration(1000);
                scaleY.setDuration(3000);
                animatorSet = new AnimatorSet();
                animatorSet.play(translationX).with(alpha).with(rotation).with(scaleY);

            }
            if(!animatorSet.isRunning()) {

                animatorSet.start();
            }

        }
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值