您好,登錄后才能下訂單哦!
這篇文章主要介紹“Android怎么實(shí)現(xiàn)網(wǎng)易云推薦歌單界面”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“Android怎么實(shí)現(xiàn)網(wǎng)易云推薦歌單界面”文章能幫助大家解決問題。
由于是一些簡單的繪制,就不一一介紹了,直接上代碼。
public class MellowImageView extends ImageView { private Paint paint; public MellowImageView(Context context) { super(context); } public MellowImageView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public MellowImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); paint=new Paint(); } /** * 繪制圓角矩形圖片 * @author jimeng */ @Override protected void onDraw(Canvas canvas) { Drawable drawable = getDrawable(); if (null != drawable) { Bitmap bitmap = getBitmapFromDrawable(drawable); Bitmap b = getRoundBitmapByShader(bitmap,getWidth(),getHeight(), 20,0); final Rect rectSrc = new Rect(0, 0, b.getWidth(), b.getHeight()); final Rect rectDest = new Rect(0,0,getWidth(),getHeight()); canvas.drawBitmap(b, rectSrc, rectDest, paint); } else { super.onDraw(canvas); } } /** * 把圖片轉(zhuǎn)換成Bitmap * @param drawable * 資源圖片 * @return 位圖 */ public static Bitmap getBitmapFromDrawable(Drawable drawable) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, drawable .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); drawable.draw(canvas); return bitmap; } public static Bitmap getRoundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius, int boarder) { if (bitmap == null) { return null; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); float widthScale = outWidth * 1f / width; float heightScale = outHeight * 1f / height; Matrix matrix = new Matrix(); matrix.setScale(widthScale, heightScale); //創(chuàng)建需要輸出的bitmap Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(desBitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); //著色器 BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); //給著色器配置matrix bitmapShader.setLocalMatrix(matrix); paint.setShader(bitmapShader); //創(chuàng)建矩形區(qū)域并且預(yù)留出border RectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder); //把傳入的bitmap繪制到圓角矩形區(qū)域內(nèi) canvas.drawRoundRect(rect, radius, radius, paint); return desBitmap; } }
效果圖如下:
時(shí)間原因,一些簡單的細(xì)節(jié)沒有畫上去。
將整個(gè)布局放在HorizontalScrollView中使其可以左右滑動(dòng),以一個(gè)item為例。
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none" android:orientation="horizontal" > <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal"> <!-- 美化,并無其他作用--> <RelativeLayout android:layout_width="@dimen/jimeng_dp_16" android:layout_height="@dimen/jimeng_dp_135"/> <RelativeLayout android:layout_width="@dimen/jimeng_dp_130" android:layout_height="@dimen/jimeng_dp_135" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/like_icon2" android:layout_centerHorizontal="true" android:text="計(jì)蒙不吃魚" android:maxLines="1" android:ellipsize="end" android:textColor="@color/jimeng_black" android:textSize="12.0dip" /> <com.shenzhen.jimeng.jmhnzsb.View.MellowImageView android:id="@+id/like_icon2" android:layout_width="120.0dip" android:layout_height="120.0dip" android:layout_centerHorizontal="true" android:scaleType="centerCrop" android:src="@drawable/yf1" /> </RelativeLayout> </LinearLayout> </HorizontalScrollView >
博主使用的是ViewFlipper。
XML代碼如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ViewFlipper android:id="@+id/viewFlipper" android:layout_width="120.0dip" android:layout_height="120.0dip" android:flipInterval="3000" android:inAnimation="@anim/anim_marquee_in" android:outAnimation="@anim/anim_marquee_out" /> </RelativeLayout>
劃重點(diǎn):兩個(gè)動(dòng)畫文件,計(jì)蒙調(diào)試的將近30分鐘才調(diào)試成類似效果
anim_marquee_in:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="500" android:fromYDelta="120%p" android:toYDelta="0"/> <scale android:duration="500" android:fromXScale="0.8" android:fromYScale="0.8" android:toXScale="1" android:toYScale="1" android:pivotY="50%" android:pivotX="50%"/> </set>
anim_marquee_out:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="500" android:fromYDelta="0" android:toYDelta="-120%p"/> <scale android:duration="500" android:fromXScale="1" android:fromYScale="1" android:toXScale="0.8" android:toYScale="0.8" android:pivotY="50%" android:pivotX="50%"> </scale> </set
在Java文件中為ViewFlipper添加view:
private ViewFlipper viewFlipper; //--------------------------------- viewFlipper.removeAllViews(); View view = View.inflate(getContext(), R.layout.home_rebroadcast_item, null); MellowImageView carouselImageView=view.findViewById(R.id.carousel_item_iv); View view1 = View.inflate(getContext(), R.layout.home_rebroadcast_item1, null); MellowImageView carouselImageView1=view.findViewById(R.id.carousel_item_iv); // 循環(huán)滾動(dòng)圖片的點(diǎn)擊事件 // iv.setOnClickListener(new ....); //添加view viewFlipper.addView(view); viewFlipper.addView(view1);
關(guān)于“Android怎么實(shí)現(xiàn)網(wǎng)易云推薦歌單界面”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。