Forráskód Böngészése

表情包显示方法用textView html方式实现

zengjiebin 7 éve
szülő
commit
4a829dedd0

+ 2 - 0
app/src/main/AndroidManifest.xml

@@ -424,6 +424,8 @@
         <activity
             android:name="com.sheep.gamegroup.view.activity.ActXiaomiGame"
             android:screenOrientation="portrait" />
+        <activity android:name="com.sheep.gamegroup.view.activity.ActTestExpression"
+            android:screenOrientation="portrait" />
         <activity
             android:name="com.sheep.gamegroup.view.activity.MiddleScreenShotAct"
             android:screenOrientation="user"

+ 30 - 0
app/src/main/java/com/sheep/gamegroup/util/ResImageGetter.java

@@ -0,0 +1,30 @@
+package com.sheep.gamegroup.util;
+
+import android.content.res.Resources;
+import android.graphics.drawable.Drawable;
+import android.text.Html;
+import android.widget.TextView;
+
+import com.sheep.jiuyan.samllsheep.BuildConfig;
+import com.sheep.jiuyan.samllsheep.SheepApp;
+
+/**
+ * Created by realicing on 2018/11/7.
+ * realicing@sina.com
+ */
+public class ResImageGetter implements Html.ImageGetter {
+
+    private int textSize;
+
+    public ResImageGetter(TextView textView) {
+        textSize = (int) textView.getTextSize() * 2;
+    }
+
+    @Override
+    public Drawable getDrawable(final String source) {
+        Resources resources = SheepApp.getInstance().getResources();
+        Drawable drawable = resources.getDrawable(resources.getIdentifier(source, "mipmap", BuildConfig.APPLICATION_ID));
+        drawable.setBounds(0, 0, textSize, textSize);
+        return drawable;
+    }
+}

+ 11 - 1
app/src/main/java/com/sheep/gamegroup/util/TestUtil.java

@@ -33,6 +33,7 @@ import com.sheep.gamegroup.view.activity.ActInstallApkList;
 import com.sheep.gamegroup.view.activity.ActMain;
 import com.sheep.gamegroup.view.activity.ActSheepApkList;
 import com.sheep.gamegroup.view.activity.ActSheepPngList;
+import com.sheep.gamegroup.view.activity.ActTestExpression;
 import com.sheep.gamegroup.view.activity.ActWeb;
 import com.sheep.gamegroup.view.activity.NotificationsUtils;
 import com.sheep.gamegroup.view.dialog.DialogGameOrTaskOrGift;
@@ -222,7 +223,7 @@ public class TestUtil {
      * @param activity
      */
     public static void test(final Activity activity) {
-        final String[] items = {"复制token","复制打点数据","从jenkins下载小绵羊安装包","龙猫竞猜","龙猫竞猜-scheme",
+        final String[] items = {"复制token","复制打点数据","从jenkins下载小绵羊安装包","测试表情包","龙猫竞猜","龙猫竞猜-scheme",
                 "有米科技测试","有米科技","手机型号测试","测试通知栏", "测试自定义通知栏","测试自定义通知栏2",
                 "开启通知栏权限0","开启通知栏权限1","开启通知栏权限2","开启通知栏权限3",
                 "开启通知栏权限","通知栏权限1","通知栏权限2","通知栏权限8.0","测试支付","测试内部h5", "测试外部h5",
@@ -239,6 +240,9 @@ public class TestUtil {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         switch (items[which]) {
+                            case "测试表情包":
+                                testExpression(activity);
+                                break;
                             case "测试通知栏":
                                 testNotification(activity);
                                 break;
@@ -528,6 +532,12 @@ public class TestUtil {
                 }).create();
         dialog.show();
     }
+
+    //测试表情包
+    private static void testExpression(Activity activity) {
+        activity.startActivity(new Intent(activity, ActTestExpression.class));
+    }
+
     //测试直接显示通知栏
     private static void testNotification(Activity activity) {
         String id = "my_channel_01";

+ 87 - 0
app/src/main/java/com/sheep/gamegroup/util/UrlImageGetter.java

@@ -0,0 +1,87 @@
+package com.sheep.gamegroup.util;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.graphics.Canvas;
+import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.Drawable;
+import android.text.Html;
+import android.widget.TextView;
+
+import com.bumptech.glide.request.target.FixedSizeDrawable;
+import com.sheep.jiuyan.samllsheep.R;
+import com.sheep.jiuyan.samllsheep.SheepApp;
+import com.sheep.jiuyan.samllsheep.utils.ClassFileHelper;
+import com.zhy.http.okhttp.OkHttpUtils;
+import com.zhy.http.okhttp.callback.FileCallBack;
+
+import java.io.File;
+
+import okhttp3.Call;
+
+/**
+ * Created by realicing on 2018/11/7.
+ * realicing@sina.com
+ */
+public class UrlImageGetter implements Html.ImageGetter {
+
+    private URLDrawable urlDrawable = null;
+    private TextView textView;
+    private int textSize;
+
+    public UrlImageGetter(TextView textView) {
+        this.textView = textView;
+        textSize = (int) textView.getTextSize() * 2;
+    }
+
+    @Override
+    public Drawable getDrawable(final String source) {
+        Resources resources = SheepApp.getInstance().getResources();
+
+        String fileName = source + ".png";
+        File file = new File(ClassFileHelper.DIR, fileName);
+        if (file.exists()) {
+            urlDrawable = new URLDrawable(resources, BitmapFactory.decodeFile(file.getAbsolutePath()));
+            urlDrawable.setBounds(0, 0, textSize, textSize);
+            return urlDrawable;
+        }
+        urlDrawable = new URLDrawable(resources, BitmapFactory.decodeResource(resources, R.drawable.loading_01, null));
+        urlDrawable.setBounds(0, 0, textSize, textSize);
+
+        OkHttpUtils.get().url(ViewUtil.getNetImgByName(source)).build().execute(new FileCallBack(ClassFileHelper.DIR, fileName) {
+            @Override
+            public void onError(Call call, Exception e, int id) {
+            }
+
+            @Override
+            public void onResponse(File response, int id) {
+                if (response.exists()) {
+                    urlDrawable.bitmap = BitmapFactory.decodeFile(response.getAbsolutePath());
+                    urlDrawable.setBounds(0, 0, textSize, textSize);
+                    textView.invalidate();
+                    textView.setText(textView.getText());//不加这句显示不出来图片,原因不详
+                }
+            }
+        });
+        return urlDrawable;
+    }
+
+    public class URLDrawable extends BitmapDrawable {
+        URLDrawable(Resources res, Bitmap bitmap) {
+            super(res, bitmap);
+        }
+
+        public Bitmap bitmap;
+
+        @Override
+        public void draw(Canvas canvas) {
+            if (bitmap != null) {
+                canvas.drawBitmap(bitmap, 0, 0, getPaint());
+            } else {
+                super.draw(canvas);
+            }
+        }
+    }
+}

+ 61 - 0
app/src/main/java/com/sheep/gamegroup/view/activity/ActTestExpression.java

@@ -0,0 +1,61 @@
+package com.sheep.gamegroup.view.activity;
+
+import android.text.Html;
+import android.view.View;
+import android.widget.EditText;
+import android.widget.TextView;
+
+import com.sheep.gamegroup.absBase.BaseActivity;
+import com.sheep.gamegroup.util.ResImageGetter;
+import com.sheep.gamegroup.util.UrlImageGetter;
+import com.sheep.gamegroup.util.ViewUtil;
+import com.sheep.jiuyan.samllsheep.R;
+import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;
+
+import java.util.Locale;
+
+import butterknife.BindView;
+
+/**
+ * Created by realicing on 2018/11/7.
+ * realicing@sina.com
+ */
+public class ActTestExpression extends BaseActivity {
+    @BindView(R.id.test_content)
+    TextView test_content;
+    @BindView(R.id.test_input_comment)
+    EditText test_input_comment;
+    @Override
+    protected int getLayoutId() {
+        return R.layout.act_test_expression;
+    }
+
+    @Override
+    public void initView() {
+
+        TitleBarUtils.getInstance().setTitle(this, "写评价")
+                .setEndTv(this, "发表", 0xFF2EBEF2, new View.OnClickListener() {
+                    @Override
+                    public void onClick(View view) {
+                        String content = test_input_comment.getText().toString();
+                        commitComment(content);
+                    }
+                })
+                .setTitleFinish(this);
+    }
+
+    //提交数据
+    private void commitComment(String content) {
+        test_content.setText(Html.fromHtml(String.format(Locale.CHINA, "%s<img src=\"%s\" />", content, "exp_666"), new ResImageGetter(test_content), null));
+    }
+
+    //点击文本弹出表情包选择框
+    public void onClickContent(View view) {
+
+    }
+    //点击x来清空输入内容
+    public void onClickXInput(View view) {
+        ViewUtil.setText(test_content);
+        ViewUtil.setText(test_input_comment);
+    }
+}

+ 2 - 4
app/src/main/res/layout/act_comment_game_app.xml

@@ -1,11 +1,9 @@
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:background="@color/white"
-    android:orientation="vertical"
-    >
+    android:orientation="vertical">
 
     <include layout="@layout/title" />
 
@@ -87,6 +85,6 @@
     <ListView
         android:id="@+id/search_app_list"
         android:layout_width="match_parent"
-        android:layout_height="match_parent"/>
+        android:layout_height="match_parent" />
 
 </LinearLayout>

+ 62 - 0
app/src/main/res/layout/act_test_expression.xml

@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:background="@color/white"
+    android:orientation="vertical">
+
+    <include layout="@layout/title" />
+
+    <TextView
+        android:id="@+id/test_content"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:onClick="onClickContent"
+        android:padding="16dp"
+        android:textColor="#ff666666"
+        android:textSize="13sp" />
+
+    <View
+        android:layout_width="match_parent"
+        android:layout_height="1dp"
+        android:layout_marginStart="16dp"
+        android:layout_marginEnd="16dp"
+        android:background="#fff5f5f5" />
+
+    <RelativeLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content">
+
+        <android.support.v7.widget.AppCompatAutoCompleteTextView
+            android:id="@+id/test_input_comment"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:background="@null"
+            android:hint="写下您的看法"
+            android:lineSpacingMultiplier="1.3"
+            android:paddingStart="16dp"
+            android:paddingTop="20dp"
+            android:paddingEnd="16dp"
+            android:paddingBottom="20dp"
+            android:textColor="#333333"
+            android:textColorHint="#ffcccccc"
+            android:textSize="13sp" />
+
+        <ImageView
+            android:layout_width="50dp"
+            android:layout_height="wrap_content"
+            android:layout_alignBottom="@id/test_input_comment"
+            android:layout_alignParentEnd="true"
+            android:layout_marginEnd="2dp"
+            android:onClick="onClickXInput"
+            android:padding="20dp"
+            android:scaleType="fitCenter"
+            android:src="@mipmap/x" />
+    </RelativeLayout>
+    <!--这里是一个系统级别的bug吗,为什么上面的输入框不能自动弹出输入法,而必须添加下面的ListView来让整个界面充满才能自动弹出输入法(不能是View...)-->
+    <ListView
+        android:id="@+id/search_app_list"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent" />
+
+</LinearLayout>

BIN
app/src/main/res/mipmap-xxhdpi/exp_666.png


BIN
app/src/main/res/mipmap-xxhdpi/exp_ccc.png


BIN
app/src/main/res/mipmap-xxhdpi/exp_ggl.png


BIN
app/src/main/res/mipmap-xxhdpi/exp_hjz.png


BIN
app/src/main/res/mipmap-xxhdpi/exp_kbjbg.png


BIN
app/src/main/res/mipmap-xxhdpi/exp_lll.png


BIN
app/src/main/res/mipmap-xxhdpi/exp_sxyj.png


BIN
app/src/main/res/mipmap-xxhdpi/exp_wxll.png


BIN
app/src/main/res/mipmap-xxhdpi/exp_xq.png


BIN
app/src/main/res/mipmap-xxhdpi/exp_yd.png


BIN
app/src/main/res/mipmap-xxhdpi/exp_yx.png