华为Scan Kit二维码扫描 更改显示(懒人取巧)

华为Scan Kit二维码扫描 默认的 视图图标文字不能更改,改动成custom 需要改动太多,目前仅是想改动顶部提示问题和打开去除打开图库按钮。


image.png

思路
1.构建新的操作的Activity继承ScanKitActivity
2.获取显示视图的View
3.遍历显示视图的View 的子view 找到显示控件的地方
4.更改显示内容。

1.ScanKitActivity源码分析
public class ScanKitActivity extends Activity {
private RemoteView remoteView;
private ImageView backBtn;
private static final String TAG = "ScanKitActivity";



protected void onCreate(Bundle var1) {
    super.onCreate(var1);
    this.requestWindowFeature(1);
    this.setContentView(layout.scankit_layout);
    int var2 = 0;

    try {
        if (this.getIntent() != null) {
            var2 = this.getIntent().getIntExtra("ScanFormatValue", 0);
        }
    } catch (NullPointerException var6) {
        a.b("ScanKitActivity", "getIntExtra can not get");
    }

    this.remoteView = new RemoteView(this, false, var2, (Rect)null);

    this.remoteView.onCreate(var1);
    ViewGroup var3 = (ViewGroup)this.findViewById(id.ll_top);
    var3.addView(this.remoteView);
 ........
 }

RemoteView 动态添加到Activity视图中,所需要改动的在RemoteView 中。

2.实践
public class ScanByHWActivity extends ScanKitActivity {
RemoteView remoteViewCurrent;
static String TAG = "ScanByHWActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initview();
}

private void initview() {
    ViewGroup var3 = (ViewGroup) this.findViewById(R.id.ll_top);
    for (int i = 0; i < var3.getChildCount(); i++) {
        View v = var3.getChildAt(i);
        if (v instanceof RemoteView) {
            remoteViewCurrent = (RemoteView) v;
            break;
        }
    }

    if (remoteViewCurrent != null) {
       printView(remoteViewCurrent);
    }

}


private void printView(ViewGroup viewGroup) {

    for (int i = 0; i < viewGroup.getChildCount(); i++) {
        View v = viewGroup.getChildAt(i);
        if (v instanceof ViewGroup) {
            Log.e(TAG, "printView:--viewgroup " + v.getClass().getSimpleName());
            printView((ViewGroup) v);

        } else {
            Log.e(TAG, "printView:- " + v.getClass().getSimpleName());

        }
    }

 }
}

先打印所有的子元素
实际输出:

 E/ScanByHWActivity: printView:--viewgroup ProviderRemoteView
 E/ScanByHWActivity: printView:--viewgroup FrameLayout
 E/ScanByHWActivity: printView:- SurfaceView
 E/ScanByHWActivity: printView:- ViewfinderView
 E/ScanByHWActivity: printView:--viewgroup RelativeLayout
 E/ScanByHWActivity: printView:- TextView
 E/ScanByHWActivity: printView:- ImageView
 E/ScanByHWActivity: printView:- ImageView
 E/ScanByHWActivity: printView:--viewgroup LinearLayout
 E/ScanByHWActivity: printView:- ImageView
 E/ScanByHWActivity: printView:- TextView

一看就知道 可以操作
debug 操作之后,成功拿到上面如图的id

 android.widget.TextView{3a0914f V.ED..... ......ID 0,0-0,0 #7f090696 app:id/title_scan}
 android.widget.ImageView{8834d7d V.ED..... ......I. 0,0-0,0 #7f090047 app:id/back_img_in}
 android.widget.ImageView{740ae22 V.ED..C.. ......I. 0,0-0,0 #7f090230 app:id/img_btn}
 android.widget.ImageView{a54e3d2 V.ED..C.. ......ID 0,0-0,0 #7f090258 app:id/ivFlash}
 android.widget.TextView{1608493 V.ED..... ......ID 0,0-0,0 #7f0901e7 app:id/flash_light_text}

是不是有点不对?明明有三处文字怎么三个图标怎么只有2个textview
继续研究发现 中间展示的文字是在 ViewfinderView里。

image.png

并且没有暴露方法,对外修改,混淆后的代码,反射有难度,

检查了下源码


image.png

scankit_scan_tip 字段,那就强制本地编译用的修改掉,缺点 重新换电脑拉取编译就又回去了。


image.png

上图就是修改后的。

至此 隐藏图库入口就简单了:不显示扫码title 和图库入口

if (remoteViewCurrent != null) {
        TextView textView = remoteViewCurrent.findViewById(R.id.title_scan);
        ImageView img = remoteViewCurrent.findViewById(R.id.img_btn);
        textView.setText("");
        img.setVisibility(View.GONE);
         
    }
image.png

你可能感兴趣的:(华为Scan Kit二维码扫描 更改显示(懒人取巧))