www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁(yè) > > 充電吧
[導(dǎo)讀]1.需求:需要實(shí)現(xiàn)在登錄頁(yè),有一個(gè)大圖緩慢滾動(dòng)2.思路:大圖本身是幾千像素的寬度,不可能直接加載,會(huì)OOM報(bào)錯(cuò);所以應(yīng)該是切成同樣高度和寬度的較小的圖片(盡量)3.資料(轉(zhuǎn)載文章):https://w

1.需求:需要實(shí)現(xiàn)在登錄頁(yè),有一個(gè)大圖緩慢滾動(dòng)

2.思路:大圖本身是幾千像素的寬度,不可能直接加載,會(huì)OOM報(bào)錯(cuò);所以應(yīng)該是切成同樣高度和寬度的較小的圖片(盡量)

3.資料(轉(zhuǎn)載文章):https://www.jianshu.com/p/f36f68c3de46,這篇文章算是思路對(duì)的了,但是跑出來(lái)的效果不是我想要的,所以需要做魔改~

4.魔改(開(kāi)玩笑~)

我先說(shuō)下上面那個(gè)大佬寫(xiě)的文章里我遇到的問(wèn)題和解決辦法,最后會(huì)貼上我自己的代碼,GIF不會(huì)弄,但是我已經(jīng)在自己的APP上實(shí)現(xiàn)了這個(gè)效果,應(yīng)該是可以的

(1)MarqueeView里的addViewInQueue方法


public void addViewInQueue(View view){ LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); lp.setMargins(viewMargin, 0, 0, 0); view.setLayoutParams(lp); mainLayout.addView(view); view.measure(0, 0);//測(cè)量view viewWidth = viewWidth + view.getMeasuredWidth() + viewMargin; } 這個(gè)方法里首先沒(méi)有限制每個(gè)圖片寬高,所以造成拉伸不一致;然后view.mesure(0,0)的做法不太好,因?yàn)樽髡呤窃谡{(diào)用的時(shí)候用了ImageView的setBackgroundResource,最后獲取的getMesuredWidth()值不是我想要的


我希望的拉伸處理辦法是高度要拉伸到和屏幕高度一致,圖片寬度也要按照這個(gè)比例拉伸,所以肯定要改這個(gè)lp參數(shù),不能是wrap.下面是我的改動(dòng):


public?void?addImageInQueue(int?drawable){
????//在不加載圖片的前提下獲得圖片的寬高,否則很容易OOM
????BitmapFactory.Options?options?=?new?BitmapFactory.Options();/**?????*?最關(guān)鍵在此,把options.inJustDecodeBounds?=?true;?????*?這里再decodeFile(),返回的bitmap為空,但此時(shí)調(diào)用options.outHeight時(shí),已經(jīng)包含了圖片的高了?????*?decodeResource道理也是一樣的?????*/????options.inJustDecodeBounds?=?true;

????Bitmap?bitmap?=?BitmapFactory.decodeResource(getResources(),?drawable,options);//?此時(shí)返回的bitmap為null/**?????*options.outHeight為原始圖片的高?????*/????int?height?=?options.outHeight;
????int?width?=?options.outWidth;

????int?imageWidth?=?width*screenHeight/height;//將圖片的高度拉伸到和屏幕高度一樣,然后獲取等比拉伸后圖片應(yīng)該的寬度

????LinearLayout.LayoutParams?lp?=?new?LinearLayout.LayoutParams(imageWidth,
????????????screenHeight);
????ImageView?view?=?new?ImageView(getContext());
????view.setLayoutParams(lp);
????/*view.setImageResource(drawable);*/
????Glide.with(getContext()).load(drawable).placeholder(R.color.white).into(view);
????view.setScaleType(ImageView.ScaleType.FIT_XY);//設(shè)置圖片填充滿控件
????mainLayout.addView(view);
????viewWidth?=?viewWidth?+?imageWidth;
}


BitmapFactory.Options 非常好用,因?yàn)槿绻苯油ㄟ^(guò)Bitmap獲取圖片的寬高,很容易OOM,用option的話實(shí)際上bitmap還為null,沒(méi)有非內(nèi)存造成壓力,特別是圖多的時(shí)候

(2).MarqueeView里startScroll()和重寫(xiě)的run()方法

要改這兩個(gè)方法,是因?yàn)槲乙獙?shí)現(xiàn)2個(gè)效果:

從左往右滑,拼起來(lái)的圖應(yīng)該先顯示最右邊的那部分,然后往右劃,一直滑到最左邊的圖,所以起始的currentX應(yīng)該是圖片總長(zhǎng)度減去屏幕寬度,

從右往左滑,一開(kāi)始就直接顯示最左邊圖的就行了全額,所以起始的currentX應(yīng)該是0

需要說(shuō)明的是,這里

mainLayout.scrollTo(currentX,?0);


這個(gè)方法,currentX如果為正:圖片起始左下角x軸是0,滾動(dòng)到正數(shù),圖片左邊有部分不顯示

current為負(fù),正好相反,圖片右邊有部分不顯示

從左往右的時(shí)候,起始currentX是正數(shù),然后遞減1個(gè)像素,,看起來(lái)圖片就是從左往右移了

負(fù)數(shù)原理相反

我覺(jué)得這個(gè)才是這個(gè)自定義View的精髓,感謝原作者的分析

(3)scroll_content的布局是有問(wèn)題的,里面沒(méi)有設(shè)置orientation


android:orientation="horizontal"



貼下我改了之后的代碼,里面設(shè)置了初始值,可以去掉,我這是自己要用


//開(kāi)始滾動(dòng)
public?void?startScroll(){
????removeCallbacks(this);

????//必須要保證所有圖片拉伸后總寬度大于屏幕寬度
????if(viewWidth?>?screenWidth){
????????viewWidth?=?viewWidth?-?screenWidth;
????????currentX?=?(scrollDirection?==?LEFT_TO_RIGHT???viewWidth?:?0);
????????if(isSetFirtsX)?currentX?=?firtsX;//如果設(shè)置了起始值,就使用起始值
????????post(this);
????}
}
@Override
public?void?run()?{
????switch?(scrollDirection){
????????case?LEFT_TO_RIGHT:
????????????mainLayout.scrollTo(currentX,?0);
????????????currentX?--;

????????????if?(currentX?==?0)?{
????????????????mainLayout.scrollTo(viewWidth,?0);
????????????????currentX?=?viewWidth;
????????????}
????????????break;
????????case?RIGHT_TO_LEFT:
????????????mainLayout.scrollTo(currentX,?0);
????????????currentX?++;

????????????if?(currentX?==?viewWidth)?{
????????????????mainLayout.scrollTo(0,?0);
????????????????currentX?=?0;
????????????}
????????????break;
????????default:
????????????break;
????}

然后我貼下完整的代碼

public?class?MarqueeView?extends?HorizontalScrollView?implements?Runnable?{

????private?Context?context;
????private?LinearLayout?mainLayout;//跑馬燈滾動(dòng)部分
????private?int?scrollSpeed?=?5;//滾動(dòng)速度
????private?int?scrollDirection?=?LEFT_TO_RIGHT;//滾動(dòng)方向
????private?int?currentX;//當(dāng)前x坐標(biāo)
????private?int?viewMargin?=?20;//View間距
????private?int?viewWidth;//View總寬度
????private?int?screenWidth;//屏幕寬度
????private?int?screenHeight;//屏幕高度

????public?static?final?int?LEFT_TO_RIGHT?=?1;
????public?static?final?int?RIGHT_TO_LEFT?=?2;

????private?boolean?isSetFirtsX?=?false;//true的時(shí)候使用起始值

????public?boolean?isSetFirtsX()?{
????????return?isSetFirtsX;
????}

????public?MarqueeView?setSetFirtsX(boolean?setFirtsX)?{
????????isSetFirtsX?=?setFirtsX;
????????return?this;
????}

????private?int?firtsX;//起始值,可以設(shè)置負(fù)數(shù)和0,根據(jù)isSetFirtsX是否為true判斷是否使用

????public?int?getFirtsX()?{
????????return?firtsX;
????}

????public?void?setFirtsX(int?firtsX)?{
????????this.firtsX?=?firtsX;
????}

????public?int?getCurrentX()?{
????????return?currentX;
????}

????public?void?setCurrentX(int?currentX)?{
????????this.currentX?=?currentX;
????}

????public?MarqueeView(Context?context)?{
????????this(context,?null);
????}

????public?MarqueeView(Context?context,?AttributeSet?attrs)?{
????????this(context,?attrs,?0);
????}

????public?MarqueeView(Context?context,?AttributeSet?attrs,?int?defStyle)?{
????????super(context,?attrs,?defStyle);
????????this.context?=?context;
????????initView();
????}

????void?initView()?{
????????WindowManager?wm?=?(WindowManager)?getContext().getSystemService(Context.WINDOW_SERVICE);
????????screenWidth?=?wm.getDefaultDisplay().getWidth();
????????screenHeight?=?wm.getDefaultDisplay().getHeight();
????????mainLayout?=?(LinearLayout)?LayoutInflater.from(context).inflate(R.layout.scroll_content,?null);
????????this.addView(mainLayout);
????}

????public?void?addImagesInQueue(Listdrawables){
????????for?(int?drawable?:?drawables){
????????????addImageInQueue(drawable);
????????}
????}

????public?void?addImageInQueue(int?drawable){
????????//在不加載圖片的前提下獲得圖片的寬高,否則很容易OOM
????????BitmapFactory.Options?options?=?new?BitmapFactory.Options();/**?????????*?最關(guān)鍵在此,把options.inJustDecodeBounds?=?true;?????????*?這里再decodeFile(),返回的bitmap為空,但此時(shí)調(diào)用options.outHeight時(shí),已經(jīng)包含了圖片的高了?????????*?decodeResource道理也是一樣的?????????*/????????options.inJustDecodeBounds?=?true;

????????Bitmap?bitmap?=?BitmapFactory.decodeResource(getResources(),?drawable,options);//?此時(shí)返回的bitmap為null/**?????????*options.outHeight為原始圖片的高?????????*/????????int?height?=?options.outHeight;
????????int?width?=?options.outWidth;

????????int?imageWidth?=?width*screenHeight/height;//將圖片的高度拉伸到和屏幕高度一樣,然后獲取等比拉伸后圖片應(yīng)該的寬度

????????LinearLayout.LayoutParams?lp?=?new?LinearLayout.LayoutParams(imageWidth,
????????????????screenHeight);
????????ImageView?view?=?new?ImageView(getContext());
????????view.setLayoutParams(lp);
????????/*view.setImageResource(drawable);*/
????????Glide.with(getContext()).load(drawable).placeholder(R.color.white).into(view);
????????view.setScaleType(ImageView.ScaleType.FIT_XY);//設(shè)置圖片填充滿控件
????????mainLayout.addView(view);
????????viewWidth?=?viewWidth?+?imageWidth;
????}

????//開(kāi)始滾動(dòng)
????public?void?startScroll(){
????????removeCallbacks(this);

????????//必須要保證所有圖片拉伸后總寬度大于屏幕寬度
????????if(viewWidth?>?screenWidth){
????????????viewWidth?=?viewWidth?-?screenWidth;
????????????currentX?=?(scrollDirection?==?LEFT_TO_RIGHT???viewWidth?:?0);
????????????if(isSetFirtsX)?currentX?=?firtsX;//如果設(shè)置了起始值,就使用起始值
????????????post(this);
????????}
????}

????//停止?jié)L動(dòng)
????public?void?stopScroll(){
????????removeCallbacks(this);
????}

????//設(shè)置View間距
????public?void?setViewMargin(int?viewMargin){
????????this.viewMargin?=?viewMargin;
????}

????//設(shè)置滾動(dòng)速度
????public?void?setScrollSpeed(int?scrollSpeed){
????????this.scrollSpeed?=?scrollSpeed;
????}

????//設(shè)置滾動(dòng)方向?默認(rèn)從左向右
????public?void?setScrollDirection(int?scrollDirection){
????????this.scrollDirection?=?scrollDirection;
????}

????@Override
????public?void?run()?{
????????switch?(scrollDirection){
????????????case?LEFT_TO_RIGHT:
????????????????mainLayout.scrollTo(currentX,?0);
????????????????currentX?--;

????????????????if?(currentX?==?0)?{
????????????????????mainLayout.scrollTo(viewWidth,?0);
????????????????????currentX?=?viewWidth;
????????????????}
????????????????break;
????????????case?RIGHT_TO_LEFT:
????????????????mainLayout.scrollTo(currentX,?0);
????????????????currentX?++;

????????????????if?(currentX?==?viewWidth)?{
????????????????????mainLayout.scrollTo(0,?0);
????????????????????currentX?=?0;
????????????????}
????????????????break;
????????????default:
????????????????break;
????????}

????????postDelayed(this,?50?/?scrollSpeed);
????}

????@Override
????public?boolean?onTouchEvent(MotionEvent?ev)?{
????????return?false;
????}
}

調(diào)用代碼也很簡(jiǎn)單


private?void?initMarqueeView(){
????Listlist?=?new?ArrayList<>();
????list.add(R.drawable.bg_first_01);
????list.add(R.drawable.bg_first_02);
????list.add(R.drawable.bg_first_03);
????list.add(R.drawable.bg_first_04);
????list.add(R.drawable.bg_first_05);
????list.add(R.drawable.bg_first_06);
????marquee_view.addImagesInQueue(list);
????marquee_view.setScrollSpeed(8);
????marquee_view.setScrollDirection(MarqueeView.RIGHT_TO_LEFT);
????marquee_view.startScroll();
}

scroll_content布局的代碼




到此結(jié)束,希望對(duì)大家有幫助,GIF真的不會(huì)搞,見(jiàn)諒

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

其他電腦(比如安卓手機(jī)/平板電腦)的屏幕壞了,你可能想在安排維修之前緊急訪問(wèn)一些東西。你可以使用android的USB OTG功能(是的,幾乎每個(gè)android都支持這個(gè)功能,你可以將鼠標(biāo)和鍵盤(pán)連接到它)。

關(guān)鍵字: USB 鼠標(biāo) Android 樹(shù)莓派

Google 宣布與中國(guó) AR 科技公司 XREAL 達(dá)成深度戰(zhàn)略合作,聯(lián)合推出全球首款專為 Android XR 平臺(tái)打造的旗艦級(jí) AR 眼鏡 Project Aura。

關(guān)鍵字: Google XREAL Android XR眼鏡 AR

繼停止維護(hù)AOSP開(kāi)源項(xiàng)目后,谷歌母公司Alphabet近日被曝在其安卓系統(tǒng)(Android)、Pixel手機(jī)以及Chrome瀏覽器等部門(mén)裁員數(shù)百人。這一舉措引發(fā)了業(yè)界的廣泛關(guān)注,也引發(fā)了對(duì)谷歌未來(lái)業(yè)務(wù)布局的諸多猜測(cè)。

關(guān)鍵字: 谷歌 AOSP Android 裁員

在本教程中,我們將使用Capacitor 6、Angular和TypeScript構(gòu)建一個(gè)Android應(yīng)用程序,該應(yīng)用程序通過(guò)串行端口連接到BleuIO USB加密狗。該應(yīng)用程序允許用戶直接從Android設(shè)備發(fā)送和接...

關(guān)鍵字: Android USB 電容器 BLE設(shè)備

早前媒體報(bào)道谷歌將停止維護(hù)Android開(kāi)源項(xiàng)目(AOSP),將Android開(kāi)發(fā)全面轉(zhuǎn)向內(nèi)部閉源分支,目前這一消息已經(jīng)得到谷歌官方確認(rèn)。

關(guān)鍵字: 谷歌 Android 開(kāi)源

本項(xiàng)目演示了如何通過(guò)OTG (on - go) USB在Android設(shè)備上使用BleuIO USB加密狗作為串行端口。使用電容器6和@adeunis/電容器-串行插件,我們建立串行連接,發(fā)送AT命令,并實(shí)時(shí)讀取響應(yīng)。該...

關(guān)鍵字: 電容器 Android 傳感器 微控制器 嵌入式系統(tǒng)

在Linux操作系統(tǒng)中,Android Debug Bridge(ADB)是一個(gè)功能強(qiáng)大的命令行工具,它允許開(kāi)發(fā)者在計(jì)算機(jī)和Android設(shè)備之間建立通信,從而進(jìn)行調(diào)試、管理、安裝應(yīng)用等操作。本文將詳細(xì)介紹在Linux系...

關(guān)鍵字: Linux系統(tǒng) Android Debug ADB

隨著Android操作系統(tǒng)的進(jìn)步,智能手機(jī)的使用日益增加。隨后,有報(bào)道稱,惡意個(gè)人和黑客利用 Android 提供的漏洞來(lái)訪問(wèn)用戶珍視的數(shù)據(jù)。例如,此類(lèi)威脅包括 2021 年針對(duì) Android 設(shè)備發(fā)布的 Flubot...

關(guān)鍵字: Android 惡意軟件

在本教程中,我們將構(gòu)建超出電子領(lǐng)域的東西。作為一名電子工程師,我們大多數(shù)人都想為我們的物聯(lián)網(wǎng)應(yīng)用程序構(gòu)建一些用戶界面,在大多數(shù)情況下,Android應(yīng)用程序?qū)⑹怯脩襞c我們的物聯(lián)網(wǎng)設(shè)備交互的正確選擇。所以,如果你想為你的物...

關(guān)鍵字: 物聯(lián)網(wǎng) Android
關(guān)閉