Skip to content
This repository has been archived by the owner on Mar 18, 2019. It is now read-only.

NO mMarkdownView.setOnElementListener? #31

Open
a8469758 opened this issue Jan 22, 2018 · 2 comments
Open

NO mMarkdownView.setOnElementListener? #31

a8469758 opened this issue Jan 22, 2018 · 2 comments

Comments

@a8469758
Copy link

NO mMarkdownView.setOnElementListener?

@a8469758
Copy link
Author

How do I get an image click event?

@HLMODRIC
Copy link

HLMODRIC commented Mar 1, 2018

public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
private MarkdownView mMarkdownView;
@SuppressLint("JavascriptInterface")
@OverRide
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
mMarkdownView = (MarkdownView )findViewById(R.id.markdown_view);
InternalStyleSheet css = new Github();
//css.addRule("body", "line-height: 1.6", "padding: 0px");
//css.addRule(".scrollup", "width: 40px", "height: 40px", "position: fixed", "bottom: 15px", "right: 15px", "visibility: hidden", "display: flex", "align-items: center", "justify-content: center", "margin: 0 !important", "line-height: 70px", "box-shadow: 0 0 4px rgba(0, 0, 0, 0.14), 0 4px 8px rgba(0, 0, 0, 0.28)", "border-radius: 50%", "color: #fff", "padding: 5px");
//css.removeRule (".scrollup");
mMarkdownView.addStyleSheet(css);

    mMarkdownView.loadMarkdown("***********");//
    
    **mMarkdownView.setWebViewClient(new WebViewClient (){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return super.shouldOverrideUrlLoading(view, url);
        }
    });
    **mMarkdownView.getSettings().setJavaScriptEnabled(true); //支持JS**
    mMarkdownView.addJavascriptInterface(new JsInterface(this), "imageClick"); //JS交互
    mMarkdownView.setOnTouchListener(this);
}

class JsInterface{
    Context context;
    public JsInterface(Context context){
        this.context = context;
    }

    //查看图片url
    @JavascriptInterface
    public void click(String url){
        Toast.makeText (context,url,Toast.LENGTH_SHORT).show ();
        Log.d ("1111",url);
    }
}

float x,y;


@Override
public boolean onTouch(View v, MotionEvent event) {
    //通过wenview的touch来响应web上的图片点击事件
    float density = getResources().getDisplayMetrics().density; //屏幕密度
    float touchX = event.getX() / density;  //必须除以屏幕密度
    float touchY = event.getY() / density;
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        x = touchX;
        y = touchY;
    }

    if(event.getAction() == MotionEvent.ACTION_UP){
        float dx = Math.abs(touchX-x);
        float dy = Math.abs(touchY-y);
        if(dx<10.0/density&&dy<10.0/density){
            clickImage(touchX,touchY);
        }
    }
    return false;
}

private void clickImage(float touchX, float touchY) {
    //通过触控的位置来获取图片URL
    String js = "javascript:(function(){" +
            "var  obj=document.elementFromPoint("+touchX+","+touchY+");"
            +"if(obj.src!=null){"+ " window.imageClick.click(obj.src);}" +
            "})()";
    mMarkdownView.loadUrl(js);
}

}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants