是否可以在phonegap web 视图之上放置一个android 动画?

发布于 2024-12-07 11:12:40 字数 2626 浏览 0 评论 0原文

我是phonegap / java的新手。因为 CSS 动画/Javascript 动画在 Android 中速度很慢,所以我尝试创建一个 Phonegap 插件,它将本机 Android 动画放置在 Phonegap 使用的 webview 的“顶部”。

示例:资源 HTML 中的时钟背景,以及一个“handRotation”插件,该插件将在 Web 视图顶部放置旋转手的原生 Android 动画。

如果不清楚,请告诉我。我将继续尝试看看这是否可能,并在这里发布我的发现,但感谢任何帮助!

***更新

我想通了。

因此,鉴于您已经完成了 Phonegap 插件教程 并且您坐在扩展插件的类中,这就是我所做的:

public PluginResult execute(String arg0, JSONArray arg1, String arg2) {
    // TODO Auto-generated method stub


    new Thread(new Runnable(){
        public void run(){
            final SampleView hands = new SampleView(webView.getContext());
            webView.post(new Runnable(){
                public void run(){
                    webView.addView(hands, new RelativeLayout.LayoutParams(
                            ViewGroup.LayoutParams.FILL_PARENT,
                            ViewGroup.LayoutParams.FILL_PARENT
                            ));
                }
            });
        }
    }).start();


    return new PluginResult(Status.OK,  webView.getId());
}

*线程/可运行内容来自 Android UI Thread Ref 中的第二个示例(任何编辑到 webView 必须由 UI 线程运行)

** 神奇的门是了解 Plugin 类中的 webView 变量,它引用回 Phonegap webview。我是在进入班级后才发现这一点的。

***SampleView 是包含旋转动画的视图。这是供参考的代码:

    public static class SampleView extends View {
    private AnimateDrawable mDrawable;

    public SampleView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);

        Drawable dr = context.getResources().getDrawable(R.drawable.handtrim);
        dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
        Log.v("test", "test");



        RotateAnimation an = new RotateAnimation(0f, 360f, 13f, 133f);

        an.setDuration(2000);
        an.setRepeatCount(-1);



        an.setInterpolator(getContext(), android.R.anim.linear_interpolator);
        an.initialize(10, 10, 10, 10);

        mDrawable = new AnimateDrawable(dr, an);
        an.startNow();
    }

    @Override protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.TRANSPARENT);
        int w = canvas.getWidth();
        int h = canvas.getHeight();
        int cx = w / 2;
        int cy = h / 2 - mDrawable.getIntrinsicHeight();

        canvas.translate(cx, cy);

        mDrawable.draw(canvas);
        invalidate();
    }
}

I'm new to phonegap / java. Because CSS Animation/Javascript animation is slow in Android, I am trying to create a Phonegap plugin that will place a native android animation on 'top' of the webview Phonegap uses.

Example: A clock background in the asset HTML, and a 'handRotation' plugin that will put an a native android animation of rotating hands on top of the webview.

If this is not clear let me know. I will continue trying to see if this is possible, and post my findings here, but any assistance is appreciated!

***UPDATE

I figured it out.

So given you have gotten through the Phonegap Plugin tutorial and you're sitting at your class that extends Plugin, here is what I did:

public PluginResult execute(String arg0, JSONArray arg1, String arg2) {
    // TODO Auto-generated method stub


    new Thread(new Runnable(){
        public void run(){
            final SampleView hands = new SampleView(webView.getContext());
            webView.post(new Runnable(){
                public void run(){
                    webView.addView(hands, new RelativeLayout.LayoutParams(
                            ViewGroup.LayoutParams.FILL_PARENT,
                            ViewGroup.LayoutParams.FILL_PARENT
                            ));
                }
            });
        }
    }).start();


    return new PluginResult(Status.OK,  webView.getId());
}

*The thread/runnable stuff is from the 2nd example in the Android UI Thread Ref (any edits to the webView must be ran by the UI Thread)

** The magic door was knowing about the webView variable in the Plugin class, which refers back to the Phonegap webview. I found out about this only after peaking into the class.

***The SampleView is a view that contains a rotationAnimation. Here is the code for reference:

    public static class SampleView extends View {
    private AnimateDrawable mDrawable;

    public SampleView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);

        Drawable dr = context.getResources().getDrawable(R.drawable.handtrim);
        dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
        Log.v("test", "test");



        RotateAnimation an = new RotateAnimation(0f, 360f, 13f, 133f);

        an.setDuration(2000);
        an.setRepeatCount(-1);



        an.setInterpolator(getContext(), android.R.anim.linear_interpolator);
        an.initialize(10, 10, 10, 10);

        mDrawable = new AnimateDrawable(dr, an);
        an.startNow();
    }

    @Override protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.TRANSPARENT);
        int w = canvas.getWidth();
        int h = canvas.getHeight();
        int cx = w / 2;
        int cy = h / 2 - mDrawable.getIntrinsicHeight();

        canvas.translate(cx, cy);

        mDrawable.draw(canvas);
        invalidate();
    }
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文