Git Product home page Git Product logo

Comments (2)

a8469758 avatar a8469758 commented on May 30, 2024

How do I get an image click event?

from markdownview.

HLMODRIC avatar HLMODRIC commented on May 30, 2024

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);
}

}

from markdownview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.