Android自定义view之网易云推荐歌单界面

简介: 本文详细介绍了如何通过自定义View实现网易云音乐推荐歌单界面的效果。首先,作者自定义了一个圆角图片控件`MellowImageView`,用于绘制圆角矩形图片。接着,通过将布局放入`HorizontalScrollView`中,实现了左右滑动功能,并使用`ViewFlipper`添加图片切换动画效果。文章提供了完整的代码示例,包括XML布局、动画文件和Java代码,最终展示了实现效果。此教程适合想了解自定义View和动画效果的开发者。

系列文章目录

Android自定义view之网易云推荐歌单界面

先来看看网易云APP的效果:

cad8d2c0e8f34edfa4629783b50e564c.gif

前言

前天有粉丝想计蒙资讯关于网易云音乐推荐歌单界面的实现,想着好久没写博客了,于是写一篇关于自定义view的文章。

一、实现

1.自定义一个圆角图片控件(也可直接使用第三方框架)

由于是一些简单的绘制,就不一一介绍了,直接上代码。

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);
        }
    }

    /**
     * 把图片转换成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);
        //创建需要输出的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);
        //创建矩形区域并且预留出border
        RectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder);
        //把传入的bitmap绘制到圆角矩形区域内
        canvas.drawRoundRect(rect, radius, radius, paint);
        return desBitmap;
    }



}
AI 代码解读

时间原因,一些简单的细节没有画上去。

2.进行布局摆设

将整个布局放在HorizontalScrollView中使其可以左右滑动,以一个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="计蒙不吃鱼"
             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 >
AI 代码解读

3.图片切换动画效果

博主使用的是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>
AI 代码解读

划重点:两个动画文件,计蒙调试的将近30分钟才调试成类似效果
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>
AI 代码解读

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
AI 代码解读

在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);

    // 循环滚动图片的点击事件
    // iv.setOnClickListener(new ....);
    //添加view
    viewFlipper.addView(view);
    viewFlipper.addView(view1);
AI 代码解读

二、实现效果展示

5454235095c64b84bc1079c2fcaa41bf.gif

三、总结

效果其实比较好实现,但是很多地方需要设置一些判断。欢迎留言。如有问题也可私信计蒙。

目录
打赏
0
65
65
0
100
分享
相关文章
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
本文介绍了一款受游戏“金铲铲之战”启发的Android自定义View——线条等待动画的实现过程。通过将布局分为10份,利用`onSizeChanged`测量最小长度,并借助画笔绘制动态线条,实现渐变伸缩效果。动画逻辑通过四个变量控制线条的增长与回退,最终形成流畅的等待动画。代码中详细展示了画笔初始化、线条绘制及动画更新的核心步骤,并提供完整源码供参考。此动画适用于加载场景,提升用户体验。
308 5
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
一篇文章带你走近Android自定义view
这是一篇关于Android自定义View的全面教程,涵盖从基础到进阶的知识点。文章首先讲解了自定义View的必要性及简单实现(如通过三个构造函数解决焦点问题),接着深入探讨Canvas绘图、自定义属性设置、动画实现等内容。还提供了具体案例,如跑马灯、折线图、太极图等。此外,文章详细解析了View绘制流程(measure、layout、draw)和事件分发机制。最后延伸至SurfaceView、GLSurfaceView、SVG动画等高级主题,并附带GitHub案例供实践。适合希望深入理解Android自定义View的开发者学习参考。
411 84
讲讲Android为自定义view提供的SurfaceView
本文详细介绍了Android中自定义View时使用SurfaceView的必要性和实现方式。首先分析了在复杂绘制逻辑和高频界面更新场景下,传统View可能引发卡顿的问题,进而引出SurfaceView作为解决方案。文章通过Android官方Demo展示了SurfaceView的基本用法,包括实现`SurfaceHolder.Callback2`接口、与Activity生命周期绑定、子线程中使用`lockCanvas()`和`unlockCanvasAndPost()`方法完成绘图操作。
Android面试高频知识点(3) 详解Android View的绘制流程
Android面试高频知识点(3) 详解Android View的绘制流程
84 2
Android View的绘制流程和原理详细解说
Android View的绘制流程和原理详细解说
163 3
Android面试高频知识点(3) 详解Android View的绘制流程
View的绘制和事件处理是两个重要的主题,上一篇《图解 Android事件分发机制》已经把事件的分发机制讲得比较详细了,这一篇是针对View的绘制,View的绘制如果你有所了解,基本分为measure、layout、draw 过程,其中比较难理解就是measure过程,所以本篇文章大幅笔地分析measure过程,相对讲得比较详细,文章也比较长,如果你对View的绘制还不是很懂,对measure过程掌握得不是很深刻,那么耐心点,看完这篇文章,相信你会有所收获的。
226 3
Android面试题自定义View之Window、ViewRootImpl和View的三大流程
Android开发中,View的三大核心流程包括measure(测量)、layout(布局)和draw(绘制)。MeasureSpec类在测量过程中起到关键作用,它结合尺寸大小和模式(EXACTLY、AT_MOST、UNSPECIFIED)来指定View应如何测量。onMeasure方法用于自定义View的测量,布局阶段,ViewGroup调用onLayout确定子元素位置,而draw阶段按照特定顺序绘制背景、内容、子元素和装饰。整个流程始于ViewRootImpl的performTraversals,该方法触发测量、布局和绘制。
251 0

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问