是否可以在phonegap web 视图之上放置一个android 动画?
我是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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论