画布上字符串的简单淡出动画,android

发布于 2025-01-06 23:43:05 字数 166 浏览 0 评论 0原文

我有一个从 View 扩展的自定义视图,其中包含以不同角度绘制的大量文本,并且我希望特定的字符串在首次启动后将其 alpha 值降低到某个水平。任何建议或片段将不胜感激:)

postInvalidateDelayed(...) 似乎不适用于此任务。

I have a custom view extending from View with lots of text drawn at different angles and I want a particular string to decrease its alpha value to a certain level once, after first start. Any suggestion or snippet would be appreciated :)

postInvalidateDelayed(...) doesn't seem to work for this task.

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

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

发布评论

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

评论(3

墟烟 2025-01-13 23:43:05

一种可能是在 FrameLayout 内部创建两个相互重叠的视图。一个视图将包含所有静态字符串,另一个视图包含您想要设置动画的字符串。然后向动画视图添加 alpha 动画就很简单了。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <package.MyNonAnimatedView
        android:id="@+id/nonAnimatedView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <package.MyAnimatedView
        android:id="@+id/animatedView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</FrameLayout>

对于要附加到动画视图的动画:

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

在 Activity 的 onCreate(Bundle) 方法中,您可以调用AnimationUtils.loadAnimation(Context, int) 来加载从 xml 资源中获取动画并将其附加到动画视图(前提是您为其提供了 id)。

One possibility might be to create two views, inside of a FrameLayout that overlap each other. One view would contain all the static strings, the other the string you want to animate. Then it would be a simple matter of adding an alpha animation to the animated view.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <package.MyNonAnimatedView
        android:id="@+id/nonAnimatedView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <package.MyAnimatedView
        android:id="@+id/animatedView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</FrameLayout>

And for an animation you would attach to the animated view:

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

From within your activity's onCreate(Bundle) method, you can call AnimationUtils.loadAnimation(Context, int) to load the animation from the xml resource and attach it to the animated view (provided you give it an id).

空名 2025-01-13 23:43:05

您可以将 Handler 添加到您的 Activity可以按指定的时间间隔发送消息。当您的活动收到来自处理程序的回调时,它可以通知视图更新您想要更改的部分。

示例:

public class myActivity extends Activity implements Handler.Callback {

    int mDelay = 100; // Update interval (milliseconds).
    Handler mHandler = new Handler(this);
    private Runnable mEvent = new Runnable() {
        @Override
        public void run() {
            mHandler.postDelayed(mEvent, mDelay);
            Message message = mHandler.obtainMessage();
            // Add arguments to message, if required.
            mHandler.sendMessage(message);
        }
    };

    @Override
    public boolean handleMessage(Message message) {
        // Your view update code.
    }

    private void start() {
        mHandler.postDelayed(mEvent, mDelay);
    }

    private void stop() {
        mHandler.removeCallbacks(mEvent);
    }
}

调用 start() 启动处理程序,stop() 停止它。确定何时停止处理程序可能会在 handleMessage(Message) 代码中。

You can add a Handler to your activity that can send messages at a specified interval. When your activity receives a callback from the handler, it can notify the view to update the parts that you want to change.

An example:

public class myActivity extends Activity implements Handler.Callback {

    int mDelay = 100; // Update interval (milliseconds).
    Handler mHandler = new Handler(this);
    private Runnable mEvent = new Runnable() {
        @Override
        public void run() {
            mHandler.postDelayed(mEvent, mDelay);
            Message message = mHandler.obtainMessage();
            // Add arguments to message, if required.
            mHandler.sendMessage(message);
        }
    };

    @Override
    public boolean handleMessage(Message message) {
        // Your view update code.
    }

    private void start() {
        mHandler.postDelayed(mEvent, mDelay);
    }

    private void stop() {
        mHandler.removeCallbacks(mEvent);
    }
}

Calling start() starts the handler, stop() stops it. Determining when to stop the handler will probably be in the handleMessage(Message) code.

2025-01-13 23:43:05

对要褪色的垂直文本边界的正确测量存在错误。这是我的 onDraw 方法

Paint myPaint = new Paint();
myPaint.setColor(Color.parseColor("#" + colorAlpha + "3AA6D0"));// initially colorAlpha is ff
Rect r = new Rect();
char[] a = "Hello World".toCharArray();
datePaint.getTextBounds(a, 0, a.length, r);// get the bound of the text, I was not calculating this correctly
canvas.drawText("Hello World", 0, 0, myPaint);// draw the text

int colorValue = Integer.parseInt(colorAlpha, 16);
    colorValue -= 20;// decrease alpha value for next call to onDraw method by postInvalidateDelayed
if (colorValue > 40) {
        colorAlpha = Integer.toHexString(colorValue);


 // this will create the effect of fade out animation
 // because each call to onDraw method is at the difference of 50 millisecond delay 
 // and in each call we are decreasing alpha value by 20.
        postInvalidateDelayed(50, r.left, r.top, r.right, r.bottom);
}

There was a mistake in the correct measurement of vertical text bounds to be faded. Here is my onDraw method

Paint myPaint = new Paint();
myPaint.setColor(Color.parseColor("#" + colorAlpha + "3AA6D0"));// initially colorAlpha is ff
Rect r = new Rect();
char[] a = "Hello World".toCharArray();
datePaint.getTextBounds(a, 0, a.length, r);// get the bound of the text, I was not calculating this correctly
canvas.drawText("Hello World", 0, 0, myPaint);// draw the text

int colorValue = Integer.parseInt(colorAlpha, 16);
    colorValue -= 20;// decrease alpha value for next call to onDraw method by postInvalidateDelayed
if (colorValue > 40) {
        colorAlpha = Integer.toHexString(colorValue);


 // this will create the effect of fade out animation
 // because each call to onDraw method is at the difference of 50 millisecond delay 
 // and in each call we are decreasing alpha value by 20.
        postInvalidateDelayed(50, r.left, r.top, r.right, r.bottom);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文