Oce*_*lue 8 android aspect-ratio android-animation
我在imageview中运行的动画拒绝保持图像帧的宽高比.SO中的以下答案非常有用,但似乎对我不起作用: 如何在ImageView中缩放图像以保持宽高比
这是代码:
private void startAnimation(){
mImageView.setAdjustViewBounds(true);
mImageView.setScaleType(ScaleType.CENTER);
mImageView.setBackgroundResource(R.anim.my_animation);
AnimationDrawable frameAnimation = (AnimationDrawable) mImageView.getBackground();
// Start the animation (looped playback by default).
frameAnimation.start();
}
Run Code Online (Sandbox Code Playgroud)
R.anim.my_animation只是一个动画列表:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/selected"
android:oneshot="false">
<item
android:drawable="@drawable/photo_1"
android:duration="100" />
<item
android:drawable="@drawable/photo__2"
android:duration="100" />
... and so on...
</animation-list>
Run Code Online (Sandbox Code Playgroud)
A.J*_*.J. 10
不是在imageview的背景中设置可绘制的动画,而是使用src将其设置在前景中,然后让动画在那里播放.如果为图像视图设置合适的比例类型,则框架动画中的所有图像都将以完整的宽高比调整大小.
private void startAnimation(){
mImageView.setAdjustViewBounds(true);
mImageView.setScaleType(ScaleType.CENTER);
mImageView.setImageDrawable(getResources().getDrawable(R.anim.my_animation));
AnimationDrawable frameAnimation = (AnimationDrawable) mImageView.getDrawable();
// Start the animation (looped playback by default).
frameAnimation.start();
}
Run Code Online (Sandbox Code Playgroud)
小智 0
这有点棘手,但很有效。我的动画列表中有两张图片
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/selected" android:oneshot="false">
<item android:drawable="@drawable/logo1" android:duration="5000" />
<item android:drawable="@drawable/logo2" android:duration="300" />
</animation-list>
Run Code Online (Sandbox Code Playgroud)
然后我添加了第三张图片(logo0),其大小与 logo1/2 相同,但完全透明。最后我使用这个 ImageView:
<ImageView
android:id="@+id/imageViewLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:layout_margin="5dp"
android:src="@drawable/logo0"
/>
Run Code Online (Sandbox Code Playgroud)
现在,我的动画保留了图片徽标*的长宽比。
代码是:
@Override
public void onCreate(Bundle savedInstanceState) {
...
imageView = (ImageView) findViewById(R.id.imageViewLogo);
imageView.setBackgroundResource(R.drawable.logo_animation);
...
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
if(hasFocus) { frameAnimation.start(); } else { frameAnimation.stop(); }
}
Run Code Online (Sandbox Code Playgroud)
它非常简单,只需要花费额外的虚拟图片给您的资源:没有额外的代码,没有复杂的计算。
| 归档时间: |
|
| 查看次数: |
3158 次 |
| 最近记录: |