使用Android SpannableString设置TextView局部文字颜色

样式文件:

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="欢迎来到枫林的小窝!"/>

代码文件:

private void initTextView() {
    TextView textView = findViewById(R.id.textView);
    SpannableString spannableString = new SpannableString(textView.getText());
    setTextColor(spannableString, "枫林的小窝", Color.BLUE);
    textView.setText(spannableString);
}

/**
 * 设置SpannableString指定文字的颜色
 *
 * @param spannableString
 * @param text
 * @param color
 */
private void setTextColor(SpannableString spannableString, String text, int color) {
    int start = spannableString.toString().indexOf(text);
    int end = start + text.length();
    spannableString.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

参考链接:

Android开发 SpannableString开发详解

Android UI——SpannableString详细解析

Android使用VLC播放摄像头RTSP流

1.修改项目build.gradle,增加maven库地址

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://dl.bintray.com/videolan/Android" }
    }
}

2.修改模块build.gradle,增加依赖

// VLC
implementation 'org.videolan.android:libvlc-all:3.1.12'

3.布局文件中加入VLCVideoLayout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <org.videolan.libvlc.util.VLCVideoLayout
        android:id="@+id/video_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

继续阅读Android使用VLC播放摄像头RTSP流

多线程下java.util.ConcurrentModificationException问题解决

private List<String> mData = new ArrayList<>();
private List<String> mCloneData;
private int count = 0;

public void test() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                for (String string : getData()) {
                    Log.i(TAG, string);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }).start();

    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                mData.add(count++ + "");
                mCloneData = null;
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}

private List<String> getData() {
    if (mCloneData == null) {
        mCloneData = (List<String>) ((ArrayList) mData).clone();
    }
    return mCloneData;
}

参考链接:

java.util.ConcurrentModificationException 异常问题详解