華為Scan Kit二維碼掃描 更改顯示(懶人取巧)

華為Scan Kit二維碼掃描 默認(rèn)的 視圖圖標(biāo)文字不能更改,改動成custom 需要改動太多,目前僅是想改動頂部提示問題和打開去除打開圖庫按鈕。


image.png

思路
1.構(gòu)建新的操作的Activity繼承ScanKitActivity
2.獲取顯示視圖的View
3.遍歷顯示視圖的View 的子view 找到顯示控件的地方
4.更改顯示內(nèi)容。

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 動態(tài)添加到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}

是不是有點不對?明明有三處文字怎么三個圖標(biāo)怎么只有2個textview
繼續(xù)研究發(fā)現(xiàn) 中間展示的文字是在 ViewfinderView里。

image.png

并且沒有暴露方法,對外修改,混淆后的代碼,反射有難度,

檢查了下源碼


image.png

scankit_scan_tip 字段,那就強制本地編譯用的修改掉,缺點 重新?lián)Q電腦拉取編譯就又回去了。


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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容