首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将位图旋转90度

如何将位图旋转90度
EN

Stack Overflow用户
提问于 2012-01-26 16:16:34
回答 7查看 186.2K关注 0票数 141

android canvas.drawBitmap(visiblePage, 0, 0, paint);中有一条语句

当我添加canvas.rotate(90)时,没有任何效果。但如果我写

代码语言:javascript
复制
canvas.rotate(90)
canvas.drawBitmap(visiblePage, 0, 0, paint);

我没有绘制位图。那么我做错了什么呢?

EN

回答 7

Stack Overflow用户

发布于 2013-02-01 19:27:30

你也可以试试这个

代码语言:javascript
复制
Matrix matrix = new Matrix();

matrix.postRotate(90);

Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapOrg, width, height, true);

Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);

然后,您可以使用旋转的图像在您的图像视图中通过

代码语言:javascript
复制
imageView.setImageBitmap(rotatedBitmap);
票数 278
EN

Stack Overflow用户

发布于 2013-04-26 00:03:33

代码语言:javascript
复制
Bitmap source = BitmapFactory.decodeResource(this.getResources(), R.drawable.your_img);

public static Bitmap RotateBitmap(Bitmap source, float angle)
{
      Matrix matrix = new Matrix();
      matrix.postRotate(angle);
      return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
票数 187
EN

Stack Overflow用户

发布于 2012-01-26 16:24:04

以下是在android中旋转图像或调整图像大小的代码

代码语言:javascript
复制
public class bitmaptest extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        LinearLayout linLayout = new LinearLayout(this);

        // load the origial BitMap (500 x 500 px)
        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
               R.drawable.android);

        int width = bitmapOrg.width();
        int height = bitmapOrg.height();
        int newWidth = 200;
        int newHeight = 200;

        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        // rotate the Bitmap
        matrix.postRotate(45);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                          width, height, matrix, true);

        // make a Drawable from Bitmap to allow to set the BitMap
        // to the ImageView, ImageButton or what ever
        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

        ImageView imageView = new ImageView(this);

        // set the Drawable on the ImageView
        imageView.setImageDrawable(bmd);

        // center the Image
        imageView.setScaleType(ScaleType.CENTER);

        // add ImageView to the Layout
        linLayout.addView(imageView,
                new LinearLayout.LayoutParams(
                      LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
                )
        );

        // set LinearLayout as ContentView
        setContentView(linLayout);
    }
}

您还可以查看此链接以了解详细信息:http://www.anddev.org/resize_and_rotate_image_-_example-t621.html

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9015372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档