播放gif之继承webView工具类

package com.l99.designer.business.chat.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.webkit.WebSettings;

public class MyGifView extends android.webkit.WebView {

	private final String HTML ="<html><body style=\"margin:0;padding:0;\"><table style=\"width:100%%;height:100%%;\"><tr><td style=\"width:100%%;height:100%%\"><img src=\"%s\" width=\"100%%\"/></td></tr></table></body></html>";   
	private final String ASSET_SRC = "file:///android_asset/%s";
	
    public MyGifView(Context context) {
        this(context, null);
    }

    /**
     * @param context
     * @param attrs
     */
    public MyGifView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    public void init(){
    	WebSettings websetting = getSettings();
    	if (websetting != null) {
    		websetting.setUseWideViewPort(true);
    		websetting.setLoadWithOverviewMode(true);
    		websetting.setSupportZoom(false); 
    		websetting.setBuiltInZoomControls(false);
    		websetting.setAllowFileAccess(true);
    		websetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
    	}
        setVerticalScrollBarEnabled(false);
        setHorizontalScrollBarEnabled(false);
    }
    
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return true;
	}

	/**
	 * 加载assets目录的gif表情
	 * @param path 
	 * 		<li>test.gif <li>dir/test.gif
	 * 
	 */
	public void loadGifFromAsset(String path){
		String data = String.format(HTML, String.format(ASSET_SRC, path));
		try {
			loadDataWithBaseURL(null, data, "text/html", "utf-8", null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}


转载于:https://my.oschina.net/cjkall/blog/195869

猜你喜欢

转载自blog.csdn.net/weixin_33938733/article/details/91756490