Android 图像旋转
我想要一个图像无限期地旋转......这意味着我想循环它。这是我的尝试,但不幸的是它不起作用。有什么建议吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不能在主线程中循环。这将立即使您的应用程序无响应。考虑使用 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.
您可以使用以下代码将图像视图旋转无限长的时间。
如果需要修复总持续时间,请说“total_duration”。并且还定义了一个周期的“持续时间”。您可以将上面的代码与自定义计数一起使用,
希望它有帮助。
You can use following code for rotating an imageview for INFINITE period of time.
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
Hope it helps.
这是旋转图像的正确方法,请看这将随机旋转图像..因为我使用随机生成器“像循环一样”在 0 度到 1000 度之间旋转图像。
而
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.
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.