package com.example.gifsample;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
//@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GIFWebView view = new GIFWebView
(this, "file:///android_asset/imageedit_ball.gif");
setContentView(view);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
package com.example.gifsample;
import android.content.Context;
import android.webkit.WebView;
public class GIFWebView extends WebView {
public GIFWebView(Context context, String path) {
super(context);
loadUrl(path);
// TODO Auto-generated constructor stub
}
}
现在我希望每当我触摸屏幕时都会在触摸事件中播放它,并且应该在完成一个循环后停止它。 我不想在动画播放时再次播放动画。动画应完成循环,然后可以在触摸事件上再次播放。
答案 0 :(得分:1)
如果您想收听MOTION_EVENTS
(onTouch()
方法),请使用此代码并将其粘贴到MainActivity
:
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
// You can also put any code here, it will be caught when you touch the screen
break;
// This event here is triggered when you lift your finger from the screen, when touching it
case MotionEvent.ACTION_UP:
GIFWebView view = new GIFWebView
(this, "file:///android_asset/imageedit_ball.gif");
setContentView(view);
break;
}
return true;
}
如果您的gif
文件仅循环一次,它将如此显示。
因为您正在实例化一个扩展WebView
的类。这些课程不会拦截onTouch
个事件。只有Activity
个类可以。