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

當(dāng)前位置:首頁(yè) > 芯聞號(hào) > 充電吧
[導(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)系該專(zhuān)欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車(chē)的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國(guó)汽車(chē)技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車(chē)工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車(chē)。 SODA V工具的開(kāi)發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車(chē) 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來(lái)越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來(lái)越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開(kāi)幕式在貴陽(yáng)舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱(chēng),數(shù)字世界的話語(yǔ)權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營(yíng)業(yè)績(jī)穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤(rùn)率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長(zhǎng) 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營(yíng)商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國(guó)電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長(zhǎng)三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡(jiǎn)稱(chēng)"軟通動(dòng)力")與長(zhǎng)三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉