日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

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

1.需求:需要實現(xiàn)在登錄頁,有一個大圖緩慢滾動

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

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

4.魔改(開玩笑~)

我先說下上面那個大佬寫的文章里我遇到的問題和解決辦法,最后會貼上我自己的代碼,GIF不會弄,但是我已經(jīng)在自己的APP上實現(xiàn)了這個效果,應(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);//測量view viewWidth = viewWidth + view.getMeasuredWidth() + viewMargin; } 這個方法里首先沒有限制每個圖片寬高,所以造成拉伸不一致;然后view.mesure(0,0)的做法不太好,因為作者是在調(diào)用的時候用了ImageView的setBackgroundResource,最后獲取的getMesuredWidth()值不是我想要的


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


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

????Bitmap?bitmap?=?BitmapFactory.decodeResource(getResources(),?drawable,options);//?此時返回的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 非常好用,因為如果直接通過Bitmap獲取圖片的寬高,很容易OOM,用option的話實際上bitmap還為null,沒有非內(nèi)存造成壓力,特別是圖多的時候

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

要改這兩個方法,是因為我要實現(xiàn)2個效果:

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

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

需要說明的是,這里

mainLayout.scrollTo(currentX,?0);


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

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

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

負數(shù)原理相反

我覺得這個才是這個自定義View的精髓,感謝原作者的分析

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


android:orientation="horizontal"



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


//開始滾動
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;//跑馬燈滾動部分
????private?int?scrollSpeed?=?5;//滾動速度
????private?int?scrollDirection?=?LEFT_TO_RIGHT;//滾動方向
????private?int?currentX;//當前x坐標
????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的時候使用起始值

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

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

????private?int?firtsX;//起始值,可以設(shè)置負數(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為空,但此時調(diào)用options.outHeight時,已經(jīng)包含了圖片的高了?????????*?decodeResource道理也是一樣的?????????*/????????options.inJustDecodeBounds?=?true;

????????Bitmap?bitmap?=?BitmapFactory.decodeResource(getResources(),?drawable,options);//?此時返回的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;
????}

????//開始滾動
????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動
????public?void?stopScroll(){
????????removeCallbacks(this);
????}

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

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

????//設(shè)置滾動方向?默認從左向右
????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)用代碼也很簡單


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é)束,希望對大家有幫助,GIF真的不會搞,見諒

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

隨著Android操作系統(tǒng)的進步,智能手機的使用日益增加。隨后,有報道稱,惡意個人和黑客利用 Android 提供的漏洞來訪問用戶珍視的數(shù)據(jù)。例如,此類威脅包括 2021 年針對 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)閉