package com.zzhoujay.markdowndemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.zzhoujay.markdown.MarkDown;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView textView = findViewById(R.id.textView);
        assert textView != null;
        textView.setMovementMethod(LinkMovementMethod.getInstance());

        final InputStream stream = getResources().openRawResource(R.raw.hello);

        textView.post(new Runnable() {
            @Override
            public void run() {
                long time = System.nanoTime();
                Spanned spanned = MarkDown.fromMarkdown(stream, new Html.ImageGetter() {
                    public static final String TAG = "Markdown";

                    @Override
                    public Drawable getDrawable(String source) {
                        Log.d(TAG, "getDrawable() called with: source = [" + source + "]");

                        Drawable drawable;
                        try {
                            drawable = drawableFromUrl(source);
                            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                        } catch (IOException e) {
                            Log.w(TAG, "can't get image", e);
                            drawable = new ColorDrawable(Color.LTGRAY);
                            drawable.setBounds(0, 0, textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight(), 400);
                        }
                        return drawable;
                    }
                }, textView);
                long useTime = System.nanoTime() - time;
                Toast.makeText(getApplicationContext(), "use time:" + useTime, Toast.LENGTH_LONG).show();
                textView.setText(spanned);
            }
        });
    }

    public static Drawable drawableFromUrl(String url) throws IOException {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        Bitmap x;

        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.connect();
        InputStream input = connection.getInputStream();

        x = BitmapFactory.decodeStream(input);
        return new BitmapDrawable(x);
    }
}

@@ -483,16 +483,72 @@
 il.Log;%0A
+import android.view.Menu;%0Aimport android.view.MenuItem;%0A
 import a
@@ -800,16 +800,49 @@
 vity %7B%0A%0A
+    private TextView mTextView;%0A%0A
     @Ove
@@ -1005,22 +1005,17 @@
         
-final 
+m
 TextView
@@ -1015,25 +1015,16 @@
 extView 
-textView 
 = findVi
@@ -1061,17 +1061,18 @@
  assert 
-t
+mT
 extView 
@@ -1080,33 +1080,34 @@
 = null;%0A        
-t
+mT
 extView.setMovem
@@ -1148,24 +1148,99 @@
 stance());%0A%0A
+        setText(R.raw.hello);%0A    %7D%0A%0A    private void setText(int resId) %7B%0A
         fina
@@ -1293,27 +1293,21 @@
 esource(
-R.raw.hello
+resId
 );%0A%0A    
@@ -1302,33 +1302,34 @@
 esId);%0A%0A        
-t
+mT
 extView.post(new
@@ -2275,17 +2275,18 @@
 s(0, 0, 
-t
+mT
 extView.
@@ -2294,25 +2294,26 @@
 etWidth() - 
-t
+mT
 extView.getP
@@ -2327,17 +2327,18 @@
 eft() - 
-t
+mT
 extView.
@@ -2470,17 +2470,18 @@
      %7D, 
-t
+mT
 extView)
@@ -2661,17 +2661,18 @@
         
-t
+mT
 extView.
@@ -3230,11 +3230,524 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public boolean onCreateOptionsMenu(Menu menu) %7B%0A        menu.add(0, R.raw.dy,       0, %22dy%22);%0A        menu.add(0, R.raw.hello,    0, %22hello%22);%0A        menu.add(0, R.raw.mark,     0, %22mark%22);%0A        menu.add(0, R.raw.sof,      0, %22sof%22);%0A        menu.add(0, R.raw.test,     0, %22test%22);%0A        menu.add(0, R.raw.tt,       0, %22tt%22);%0A        return true;%0A    %7D%0A%0A    @Override%0A    public boolean onOptionsItemSelected(MenuItem item) %7B%0A        setText(item.getItemId());%0A        return true;%0A    %7D
 %0A%7D%0A

improve demo