简体   繁体   English

如何设置RotateAnimation的半径

[英]How to set radius of RotateAnimation

I am able to rate an image pointing to center of the screen. 我能够对指向屏幕中心的图像进行评分。 But it rotates in big radius, partially keeps off the screen. 但是它以较大的半径旋转,部分不遮挡屏幕。 How to set radius like say 100 px. 如何设置半径,例如说100像素。

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

    screenWidthPixel = displaymetrics.widthPixels;
    screenHeightPixel = displaymetrics.heightPixels;

    center_x = screenWidthPixel/2;
    center_y = screenHeightPixel/2;

    //RotateAnimation rotate = new RotateAnimation(0, 360, Animation.ABSOLUTE, center_x, Animation.ABSOLUTE, center_y);
    RotateAnimation rotate = new RotateAnimation(0, 360, center_x, center_y);
    rotate.setDuration(2000);
    rotate.setRepeatCount(Animation.INFINITE);
    rotate.setInterpolator(new LinearInterpolator());

    ImageView image= (ImageView) findViewById(R.id.test_img);

    image.setAnimation(rotate);

I am looking something like rotate.setRadius(100) 我正在寻找类似rotate.setRadius(100)东西

Try this: 尝试这个:

Remove center_x, center_y parameters from RotateAnimation() and instead set ImageView in center of the screen in layout.xml . RotateAnimation()删除center_x,center_y参数,并在layout.xml的屏幕中央设置ImageView

Activity.java Activity.java

 RotateAnimation rotate = new RotateAnimation(0, 360);
 rotate.setDuration(2000);
 rotate.setRepeatCount(Animation.INFINITE);
 rotate.setInterpolator(new LinearInterpolator());
 ImageView image= (ImageView) findViewById(R.id.test_img);
 image.setAnimation(rotate);

layout.xml layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/test_img"
        android:src="@mipmap/ic_launcher"
        android:layout_centerInParent="true"
        />
</RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM