Is it possible to place an android animation on top of the phonegap web view?
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 开发者_Go百科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();
}
}
精彩评论