zoukankan      html  css  js  c++  java
  • 设置背景图时防止图片拉伸的解决方法

    在设置背景图时,如果图片不够大会被拉伸,使图片失真,如果图片太大会对view控件的显示造成影响。
    如果只是在ImageView中设置图片的话,在程式中可以利用setScaleType进行动态设定,在xml中可以简单的用android:scaleType来设定。
    (android:scaleType="CENTER_INSIDE" 图片比View小,图片不会拉伸
     android:scaleType="CENTER_CROP"  图片比View大,View不被拉伸  其他属性的设置以后慢慢在研究。)
    现在要作的是设置LinearLayout的background时如何让图片自适应屏幕的大小,包含屏幕旋转时的调整。
    程式代码如下:

    private Drawable createImage(File imageFile) {
            try {
                // 取得当前屏幕的长宽
                DisplayMetrics dm = new DisplayMetrics();
                this.getWindowManager().getDefaultDisplay().getMetrics(dm);
                float screenWidth = dm.widthPixels;
                float screenHeight = dm.heightPixels;
                
                // 取得图片的大小并计算缩放比例
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(new FileInputStream(imageFile), null, o);
                float bitmapWidth = o.outWidth;
                float bitmapHeight = o.outHeight;
                float scale = (screenWidth / bitmapWidth < screenHeight
                        / bitmapHeight) ? screenWidth / bitmapWidth : screenHeight
                        / bitmapHeight;
    
                // 图片缩小放大
                Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(
                        imageFile));
                Matrix matrix = new Matrix();
                matrix.postScale(scale, scale);
                Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                        bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                bitmap.recycle();
                // 绘制背景图片
                Bitmap mBitmap = Bitmap.createBitmap((int) screenWidth,
                        (int) screenHeight, Bitmap.Config.RGB_565);
                Canvas mCanvas = new Canvas(mBitmap);
                Paint BitmapPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
                // 设定背景颜色
                mCanvas.drawColor(0xff000000);
                mCanvas.drawBitmap(resizedBitmap, screenWidth / 2 - bitmapWidth
                        * scale / 2, screenHeight / 2 - bitmapHeight * scale / 2,
                        BitmapPaint);
                mCanvas.save();
                BitmapDrawable drawable = new BitmapDrawable(mBitmap);
                resizedBitmap.recycle();
                return drawable;
            } catch (FileNotFoundException e) {
    
            }
            return null;
        }

    调用方法为:

        try {
                Drawable image = createImage(imageFile);
                background.setBackgroundDrawable(image);
            } catch (Exception e) {
                Log.e("Exception", e.toString());
                return false;
            }

    //===========================================================================
    在上面的程式中用到了Matrix进行图片的放大和缩小。
    使用BitmapFactory.decodeStream的option的话只能放大或缩小整数倍(使用方法在其他文章中有介绍了)
    Matrix的功能非常强大,不止可以放大缩小,还可以设置透明度等,Matrix的操作,总共分为translate(平移),rotate(旋转),scale(缩放)和skew(倾斜)四种。
    具体使用方法参考网址:
    http://blog.csdn.net/hui_ttoo/article/details/6202762
    http://liliang1222.iteye.com/blog/1152474
    http://www.moandroid.com/?p=1781
    http://www.cnblogs.com/leon19870907/articles/1978065.html
    http://www.r-base.net/archives/148
    http://my.oschina.net/amigos/blog/59598
    // ==========================================================================
    使用到了Canvas用来根据之前的图片的缩放比例配合背景画一张新的背景图,用来显示。
    具体用法可以参考网址。
    http://aina-hk55hk.iteye.com/blog/690921
    // ==========================================================================
    另外在查资料过程中,还发现另一种制作图片效果的用法。
    参考网址:http://blog.csdn.net/pgmsoul/article/details/7073332

    BitmapDrawable drawable = new BitmapDrawable(mBitmap);
    drawable.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
    drawable.setDither(true);

    可以实现图片平铺(TileMode.REPEAT)和倒影效果(TileMode.MIRROR),在setTileModeXY中设置不同的参数。
    还有另外一种TileMode.CLAMP,边缘拉伸效果,不知道在什么情况下会用到。
    // =========================================================================
    另外在制作背景图时,可以利用draw9patch来制作不失真背景。
    例如textView添加边框,可以利用.9.png。
    具体可以参考网址:http://archive.cnblogs.com/a/2017591/

  • 相关阅读:
    介绍一篇关于session的好文章,写的很详细
    介绍一篇关于session的好文章,写的很详细
    Web文件的ContentType类型大全
    介绍一篇关于session的好文章,写的很详细
    C++面向对象学习1
    归并排序,不错~~
    在博客园写给自己
    简单的数字图形
    再不写博客就老了
    python日志按时间切分TimedRotatingFileHandler
  • 原文地址:https://www.cnblogs.com/sipher/p/2575264.html
Copyright © 2011-2022 走看看