绘图使用Canvas半透明的位图(安卓) [英] Drawing translucent bitmaps using Canvas (Android)

查看:447
本文介绍了绘图使用Canvas半透明的位图(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位图对象,并希望将其与不同的半透明的水平呈现到一个Canvas对象(也就是使整个位图部分看透)。例如,我在一场比赛(被绘制在一个位图的背景顶部)精灵,我想从不透明到无形之中淡出。我能做到这一点,而不必诉诸的OpenGL?

I have a Bitmap object and want to render it to a Canvas object with varying levels of translucency (i.e. make the whole bitmap partially see through). For example, I have sprites in a game (that are drawn over the top of a bitmap background) that I want to fade out from being opaque to being invisible. Can I do this without having to resort to OpenGL?

推荐答案

您应该能够定义一个补间动画和应用该动画的ImageView的。 XML资源将是类似于以下(名为fade_animation.xml):

You should be able to define a tween animation and apply that animation to an imageView. The XML resource would be similar to the following (named fade_animation.xml):

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
   android:interpolator="@android:anim/linear_interpolator"
   android:fromAlpha="1.0"
   android:toAlpha="0.0"
   android:duration="100" />

那么你会加载和应用该动画自定义ImageView的时候准备好:

then you would load and apply this animation to your custom imageView when ready:

ImageView sprite = (ImageView) findViewById(R.id.sprite);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.fade_animation);
sprite.startAnimation(animation);

有其他的选择了。如果你正在使用位图和你不想做动画,那么你可以手动减少每一帧的Alpha值,有点像这样的:

There are other options too. If you are using bitmaps and you don't want to do an animation, then you can manually decrease the alpha value of each frame, somewhat like this:

paint.setAlpha(100);
canvas.drawBitmap(spriteImage, left, top, paint);

您是否尝试过其中的任何选项?

Did you try any of these options?

这篇关于绘图使用Canvas半透明的位图(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆