Android 图像旋转

发布于 2024-10-06 10:15:18 字数 1417 浏览 4 评论 0原文

我想要一个图像无限期地旋转......这意味着我想循环它。这是我的尝试,但不幸的是它不起作用。有什么建议吗?

package com.android.test;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;

public class imagerotate extends Activity {
 int x=1;
 int y=3;

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         LinearLayout linearLayout = new LinearLayout(this);

         while (y==3) {
   Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);

   int width = bitmap.getWidth();
   int height = bitmap.getHeight();

   Matrix matrix = new Matrix();
   matrix.postRotate(x);

   Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,height, matrix, true);
   BitmapDrawable bmd = new BitmapDrawable(rotatedBitmap);

   ImageView imageView = new ImageView(this);
   imageView.setImageDrawable(bmd);
   imageView.setScaleType(ScaleType.CENTER);

   linearLayout.addView(imageView, new LinearLayout.LayoutParams(
     LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
   setContentView(linearLayout);

   x+=1;
   }
     }
 }

I want an image to rotate for an indefinite time........which means I want to loop it. This is my attempt but unfortunately it does not work. Any suggestions?

package com.android.test;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;

public class imagerotate extends Activity {
 int x=1;
 int y=3;

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         LinearLayout linearLayout = new LinearLayout(this);

         while (y==3) {
   Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);

   int width = bitmap.getWidth();
   int height = bitmap.getHeight();

   Matrix matrix = new Matrix();
   matrix.postRotate(x);

   Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,height, matrix, true);
   BitmapDrawable bmd = new BitmapDrawable(rotatedBitmap);

   ImageView imageView = new ImageView(this);
   imageView.setImageDrawable(bmd);
   imageView.setScaleType(ScaleType.CENTER);

   linearLayout.addView(imageView, new LinearLayout.LayoutParams(
     LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
   setContentView(linearLayout);

   x+=1;
   }
     }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妥活 2024-10-13 10:15:18

你不能在主线程中循环。这将立即使您的应用程序无响应。考虑使用 RotateAnimation - 请参阅文档链接。

You can't loop in the main thread. This will immediately make your app unresponsive. Consider using a RotateAnimation - see the link for documentation.

请止步禁区 2024-10-13 10:15:18

您可以使用以下代码将图像视图旋转无限长的时间。

Animation rotateAnim = new RotateAnimation(0, 360);
rotateAnim.setDuration(5000);
rotateAnim.setRepeatCount(Animation.INFINITE);
rotateAnim.setInterpolator(new AccelerateInterpolator());
rotateAnim.setRepeatMode(Animation.REVERSE);
img.startAnimation(rotateAnim);

如果需要修复总持续时间,请说“total_duration”。并且还定义了一个周期的“持续时间”。您可以将上面的代码与自定义计数一起使用,

int count = total_duration/duration;
Animation rotateAnim = new RotateAnimation(0, 360);
rotateAnim.setDuration(duration);
rotateAnim.setRepeatCount(count);
rotateAnim.setInterpolator(new AccelerateInterpolator());
rotateAnim.setRepeatMode(Animation.REVERSE);
img.startAnimation(rotateAnim);

希望它有帮助。

You can use following code for rotating an imageview for INFINITE period of time.

Animation rotateAnim = new RotateAnimation(0, 360);
rotateAnim.setDuration(5000);
rotateAnim.setRepeatCount(Animation.INFINITE);
rotateAnim.setInterpolator(new AccelerateInterpolator());
rotateAnim.setRepeatMode(Animation.REVERSE);
img.startAnimation(rotateAnim);

If your total duration needs to be fix, say "total_duration". And "duration" for one cycle is also defined. You can use the above code with custom count as

int count = total_duration/duration;
Animation rotateAnim = new RotateAnimation(0, 360);
rotateAnim.setDuration(duration);
rotateAnim.setRepeatCount(count);
rotateAnim.setInterpolator(new AccelerateInterpolator());
rotateAnim.setRepeatMode(Animation.REVERSE);
img.startAnimation(rotateAnim);

Hope it helps.

念三年u 2024-10-13 10:15:18

这是旋转图像的正确方法,请看这将随机旋转图像..因为我使用随机生成器“像循环一样”在 0 度到 1000 度之间旋转图像。

    Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        Matrix mtx = new Matrix();
        mtx.postRotate(n);
        bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
                bmp.getHeight(), mtx, true);
        contentView.setImageBitmap(bmp);  

bmp是你想要旋转的位图。
为了让您正常旋转图像,您只需更改 mtx.postRotate(n);
mtx.postRotate(90); 添加到按钮或菜单设置。由你决定。
干杯。
编辑:PS contentView 指的是您的 ImageView。

well here is the proper way to rotate an image , please see this is will rotate the image randomly.. as i'm using random generator "like a loop" to rotate the image between 0 degree to 1000 degree.

    Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        Matrix mtx = new Matrix();
        mtx.postRotate(n);
        bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
                bmp.getHeight(), mtx, true);
        contentView.setImageBitmap(bmp);  

while bmp is the Bitmap you wanna rotate.
in order for you to rotate the image normally you just change the mtx.postRotate(n);
to mtx.postRotate(90); add to the button or to menu settings. its up to you.
cheers.
edit : P.S contentView refers to your ImageView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文