BlackBerry平台图片缩放功能-Bitmap
2010-08-08 12:25
218 查看
BlackBerry 5.0 SDK支持图片缩放,类Bitmap提供了对图片缩放的支持。 1. Bitmap.scaleInto(Bitmap dst, int filterType)
public void scaleInto(Bitmap dst, int filterType)把原来的图片缩放成Bitmap dst的比例。Parameters:
dst- Destination bitmap.
filterType- Interpolation filter type. May be one of:
Bitmap.FILTER_LANCZOS
Bitmap.FILTER_BOX
Bitmap.FILTER_BILINEARThrows:
NullPointerException- Thrown if 'dst' is null.
IllegalArgumentException- Thrown if the destination bitmap is read-only.
IllegalArgumentException- Thrown if illegal filter type is specified.
Bitmap Scaling
各种图片缩放范例:testBitmap0 = Bitmap.getBitmapResource("rim.png"); // Create different scaled versions of the original bitmap Bitmap scaledBitmap1 = new Bitmap(200, 50); Bitmap scaledBitmap2 = new Bitmap(200, 75); Bitmap scaledBitmap3 = new Bitmap(40, 60); Bitmap scaledBitmap4 = new Bitmap(200, 50); Bitmap scaledBitmap5 = new Bitmap(40, 60); Bitmap scaledBitmap6 = new Bitmap(200, 40); // Example 1. Scale the source bitmap and store it in another bitmap testBitmap0.scaleInto(scaledBitmap1, Bitmap.FILTER_BILINEAR); // Example 2. Copy fragments of the original bitmap into fragments of another bitmap testBitmap0.scaleInto(0, 0, testBitmap0.getWidth(), testBitmap0.getHeight(), scaledBitmap2, 0, 0, 50, 50, Bitmap.FILTER_BILINEAR); testBitmap0.scaleInto( 0, 0, 50, 50, scaledBitmap2, 75, 15, 50, 50, Bitmap.FILTER_BILINEAR); testBitmap0.scaleInto(50, 0, 50, 50, scaledBitmap2, 150, 0, 50, 50, Bitmap.FILTER_BILINEAR); // Example 3. Scale preserving the aspect ratio. Fit horizontally. testBitmap0.scaleInto(scaledBitmap3, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FIT); // Example 4. Scale preserving the aspect ratio. Fit vertically. testBitmap0.scaleInto(scaledBitmap4, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FIT); // Example 5. Scale preserving the aspect ratio. Fill vertically. testBitmap0.scaleInto(scaledBitmap5, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FILL); // Example 6. Scale preserving the aspect ratio. Fill horizontally. testBitmap0.scaleInto(scaledBitmap6, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FILL);
各种图片缩放结果如下:过滤参数包括:
Bitmap.FILTER_LANCZOS- Lanczos interpolation is considered to give best image quality, but it is the slowest out the other three interpolation algorithms. Lanczos interpolation produces the sharpest images, but may also introduce some ringing artifacts.
Bitmap.FILTER_BOX- Box filter (aka mean filter, averaging filter, and smoothing filter) produces blurry images and is considered to have poor visual results. Very fast computation.
Bitmap.FILTER_BILINEAR- Bilinear filter produces good results for image reduction and enlargement, but displays sharp transition lines. Very fast computation. This filter is popular in rendering of the previews.各种过滤参数使用后图片结果: 2. Bitmap.scaleInto(Bitmap dst, int filterType, int iAspectRatioOption)
public void scaleInto(Bitmap dst,int filterType,int iAspectRatioOption)Parameters:
dst- Destination bitmap.
filterType- Interpolation filter type. May be one of:
Bitmap.FILTER_LANCZOS
Bitmap.FILTER_BOX
Bitmap.FILTER_BILINEAR
preserveAspectRatio- - preserve aspect ratio option. May be one of:
Bitmap.SCALE_STRETCH- aspect ratio is not preserved
Bitmap.SCALE_TO_FIT?preserve aspect ratio and fit the source bitmap to the destination bitmap
Bitmap.SCALE_TO_FILL?preserve aspect ratio and fill the destination bitmap with the source bitmapThrows:
NullPointerException- Thrown if 'dst' is null.
IllegalArgumentException- Thrown if the preserveAspectRatio parameter is invalid.
IllegalArgumentException- Thrown if the destination bitmap is read-only.
IllegalArgumentException- Thrown if illegal filter type is specified.SCALE_TO_FIT ?With this option the aspect ratio is preserved. In case the source and the output bitmaps have different aspect ratios the scale is MIN((height_out/height_in), (width_out/width_in)). In other words, it will scale to match the smallest destination dimension (height or width). The source bitmap data is rendered in the center of the output bitmap. The source bitmap is fit to the dimensions of the destination bitmap and a part of destination bitmap remains unchanged.下图是ScaleApplication的运行截图:
ScaleApplication 源代码如下:
package com.rim.samples.device.ui.bitmapscalingdemo;
import net.rim.device.api.system.*;import net.rim.device.api.ui.*;import net.rim.device.api.ui.container.*;import net.rim.device.api.ui.component.*;
/** * A sample application to demonstrate Bitmap scaling */public class BitmapScalingDemo extends UiApplication{ /** * Entry point for application * @param args Command line arguments (not used) */ public static void main(String[] args) { // Create a new instance of the application and make the currently // running thread the application's event dispatch thread. BitmapScalingDemo app = new BitmapScalingDemo(); app.enterEventDispatcher(); }
/** * Creates a new BitmapScalingDemo object */ public BitmapScalingDemo() { pushScreen(new BitmapScalingDemoScreen()); }
/** * MainScreen class for the BitmapScalingDemo application */ static class BitmapScalingDemoScreen extends MainScreen { private static String LABEL_X = " x "; /** * Creates a new BitmapScalingDemoScreen object */ BitmapScalingDemoScreen() { setTitle("Bitmap Scaling Demo"); // Create a Bitmap from a project resource Bitmap bitmapOrig = Bitmap.getBitmapResource("rim.png"); // Create a Bitmap of arbitrary size int scaledX = 175; int scaledY = 50; Bitmap bitmapScaled = new Bitmap(scaledX, scaledY); // Scale the original Bitmap into the new Bitmap using // a Lanczos filter. bitmapOrig.scaleInto(bitmapScaled, Bitmap.FILTER_LANCZOS); // Display the original Bitmap on the screen BitmapField bitmapFieldOrig = new BitmapField(bitmapOrig, Field.FOCUSABLE); StringBuffer strBuff = new StringBuffer("Original - "); strBuff.append(bitmapOrig.getWidth()); strBuff.append(LABEL_X); strBuff.append(bitmapOrig.getHeight()); add(new LabelField(strBuff.toString())); add(bitmapFieldOrig); add(new SeparatorField()); // Display the scaled Bitmap on the screen BitmapField bitmapFieldScaled1 = new BitmapField(bitmapScaled, Field.FOCUSABLE); strBuff.delete(0, strBuff.length()); strBuff.append("/nScaled - "); strBuff.append(bitmapScaled.getWidth()); strBuff.append(LABEL_X); strBuff.append(bitmapScaled.getHeight()); strBuff.append(" - FILTER_LANCZOS - Aspect ratio not preserved"); add(new LabelField(strBuff.toString())); add(bitmapFieldScaled1); add(new SeparatorField()); // Redefine the scaled Bitmap bitmapScaled = new Bitmap(scaledX, scaledY); // Scale the original Bitmap into the new Bitmap using // a bilinear filter and maintaining aspect ratio. bitmapOrig.scaleInto(bitmapScaled, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FILL); // Display the newly scaled Bitmap on the screen BitmapField bitmapFieldScaled2 = new BitmapField(bitmapScaled, Field.FOCUSABLE); strBuff.delete(0, strBuff.length()); strBuff.append("/nScaled - "); strBuff.append(bitmapScaled.getWidth()); strBuff.append(LABEL_X); strBuff.append(bitmapScaled.getHeight()); strBuff.append(" - FILTER_BILINEAR - Aspect ratio preserved"); add(new LabelField(strBuff.toString())); add(bitmapFieldScaled2); add(new SeparatorField()); // Redefine the scaled Bitmap bitmapScaled = new Bitmap(scaledX, scaledY); // Calculate fragment dimensions int fragmentWidth = bitmapOrig.getWidth() >> 1; // >> 1 equivalent to / 2 int fragmentHeight = bitmapOrig.getHeight() >> 1; // >> 1 equivalent to / 2 // Scale a fragment of the original Bitmap into the new Bitmap // using a box filter. bitmapOrig.scaleInto(0, 0, fragmentWidth, fragmentHeight, bitmapScaled, 0, 0, bitmapScaled.getWidth(), bitmapScaled.getHeight(), Bitmap.FILTER_BOX); // Display the newly scaled Bitmap on the screen BitmapField bitmapFieldScaled3 = new BitmapField(bitmapScaled, Field.FOCUSABLE); strBuff.delete(0, strBuff.length()); strBuff.append("/nScaled fragment "); strBuff.append(fragmentWidth); strBuff.append(LABEL_X); strBuff.append(fragmentHeight); strBuff.append(" into "); strBuff.append(bitmapScaled.getWidth()); strBuff.append(LABEL_X); strBuff.append(bitmapScaled.getHeight()); strBuff.append(" - FILTER_BOX"); add(new LabelField(strBuff.toString())); add(bitmapFieldScaled3); } }}
相关文章推荐
- 【iOS开发-54】案例学习:通过UIScrollView的缩放图片功能练习代理模式的具体实现
- .Net语言 APP开发平台——Smobiler学习日志:快速实现应用中的图片、声音等文件上传功能
- mui中图片手势缩放功能的实现
- android photoview 图片放大缩放功能 ImageView
- Android中几种图像特效处理的小技巧,比如圆角,倒影,还有就是图片缩放,Drawable转化为Bitmap,Bitmap转化为Drawable等等
- ImageView实现图片旋转和缩放功能
- Android实战简易教程-第九枪(BitmapFactory.Options对资源图片进行缩放)
- android photoview 图片放大缩放功能 ImageView
- php实现图片缩放功能类
- 利用WeX5平台实现单图片上传功能
- Android控件实现图片缩放功能
- PHP图片处理类实现缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色等功能
- 在android平台缩放和旋转图片
- 修改图像大小的方法,IOS图片缩放功能
- 实习第八天:surfaceView实现图片缩放拖动功能
- JAVA实现gif图片缩放与剪切功能
- Jquery图片的缩放功能
- [置顶] viewPager+photoView实现图片轮播和手势缩放功能 支持手势缩放的imageView 如何实现相册左右滑动和手势缩放 如何让图片自适应控件大小 photoView如何使用(上)
- iOS开发UI篇—UIScrollView控件实现图片缩放功能
- Android 把bitmap转换成String,计算图片的缩放值,根据路径获得突破并压缩返回bitmap用于显示,获取保存 隐患检查的图片文件夹名称,获取保存图片的目录,根据路径删除图片,添加到图库