为ScrollView增加圆角的三种方式,及自定义属性【在Linearlayout中新增ScrollView支持滚动 后续】...

获取圆角的几种方案如下:
方案一:
通过shape来实现,给scrollView增加背景来实现
方案二:
通过自定义ScrollView,还要自定义属性,在dispatchDraw中不停的裁剪
方案三:
用Android 5.0新增的接口,给ScrollView添加setOutlineProvider监听来实现

 

【注意】:设置圆角时已经要给scrollview设置padding值,不然圆角没效果

demo:
方案一:

首先在res/drawable文件夹下,新建一个文件,命名为:shape_radius.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:radius="@dimen/dialog_keyboard_setting_round"/>
    <solid android:color="#31e10a"/>

</shape>

 

然后给scrollview新增background为上面的文件就行了

eg:https://www.cnblogs.com/MianActivity/p/5867776.html

方案二:
自定义布局:

package com.smartisanos.sara.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import com.smartisanos.sara.R;

public class RoundedRectLinearLayout extends LinearLayout {
    private Path mClip;
    private float mRadius;
    private float mRadiusMarginTop;
    private float mRadiusMarginLeft;
    private float mRadiusMargeinRight;
    private float mRadiusMargeinBottom;

    public RoundedRectLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public RoundedRectLinearLayout(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RoundedRectListView, 0, 0);
            mRadius = a.getDimensionPixelSize(R.styleable.RoundedRectListView_radius, 0);
            mRadiusMarginTop = a.getDimensionPixelSize(R.styleable.RoundedRectListView_radius_marginTop, 0);
            mRadiusMarginLeft = a.getDimensionPixelSize(R.styleable.RoundedRectListView_radius_marginLeft, 0);
            mRadiusMargeinRight = a.getDimensionPixelSize(R.styleable.RoundedRectListView_radius_marginRight, 0);
            mRadiusMargeinBottom = a.getDimensionPixelSize(R.styleable.RoundedRectListView_radius_marginBottom, 0);
            a.recycle();
        }
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (mRadius > 0) {
            mClip = new Path();
            RectF rectRound = new RectF(mRadiusMarginLeft, mRadiusMarginTop, w
                    - mRadiusMargeinRight, h - mRadiusMargeinBottom);
            mClip.addRoundRect(rectRound, mRadius, mRadius, Direction.CW);
        }
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        int saveCount = canvas.save();
        if (mRadius > 0) {
            canvas.clipPath(mClip);
        }
        super.dispatchDraw(canvas);
        canvas.restoreToCount(saveCount);
    }
}

 

自定义属性:

在res/values/attrs.xml中

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="RoundedRectListView">
        <attr name="radius" format="dimension" />
        <attr name="radius_marginTop" format="dimension" />
        <attr name="radius_marginLeft" format="dimension" />
        <attr name="radius_marginRight" format="dimension" />
        <attr name="radius_marginBottom" format="dimension" />
    </declare-styleable>
</resources>

 

布局中:

把LinearLayout改为com.smartisanos.sara.widget.RoundedRectLinearLayout,同时:

 
  
<com.smartisanos.sara.widget.LocalSearchLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="match_parent" >


<com.smartisanos.sara.widget.RoundedRectLinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/search_result_bg" android:orientation="vertical" android:paddingTop="7dp" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" app:radius="12dp" app:radius_marginLeft="@dimen/local_search_rect_margin" app:radius_marginRight="@dimen/local_search_rect_margin" app:radius_marginBottom="20dp" > …… </com.smartisanos.sara.widget.RoundedRectLinearLayout>



</com.smartisanos.sara.widget.LocalSearchLayout>
 

 

 

方案三:

        mSettingRound = IMEContext.getContext().getResources().getDimensionPixelSize(R.dimen.dialog_keyboard_setting_round);
        mScrollView = (ScrollView) mRootView.findViewById(R.id.dialog_keyboard_setting_scroll);
        if (Build.VERSION.SDK_INT >= 21) {
            mScrollView.setOutlineProvider(new ViewOutlineProvider() {
                @Override
                public void getOutline(View view, Outline outline) {
                    if (Build.VERSION.SDK_INT >= 21) {
                        outline.setRoundRect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom(), mSettingRound);
                    }
                }
            });
            mScrollView.setClipToOutline(true);
        }

 

转载于:https://www.cnblogs.com/longjunhao/p/9056128.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Unity 2020.3.33f1版本,我们可以使用三个Image组件来实现无限滚动的列表。具体步骤如下: 1. 创建一个空的GameObject作为ScrollView的父级容器,并将它命名为ScrollView。 2. 在ScrollView下创建一个Content的子对象,并将其命名为Content。Content将作为滚动的内容区域。 3. 在Content下创建三个Image对象,并命名为Image1、Image2和Image3。这三个Image对象将用来实现无限滚动的效果。 4. 在ScrollView的RectTransform组件上,将Pivot设置为(0, 1),以便将ScrollView的原点设置在左上角。 5. 设置Content的RectTransform组件的Pivot为(0, 1),将其原点设置在左上角。 6. 在ScrollView的RectTransform组件上,调整Size Delta的值,以便调整ScrollView的大小。确保内容区域足够容纳三个Image对象。 7. 在ScrollView上添加Scrollbar和Scroll Rect组件,并将Scroll Rect组件的Content属性设置为步骤2创建的Content对象。 8. 创建一个C#脚本,并将其附加到ScrollView对象上。在脚本,编写相关的逻辑代码以实现无限滚动的效果。 逻辑代码的实现思路如下: a. 在Start()方法,获取Image1、Image2和Image3的RectTransform组件,并记录它们的初始位置。 b. 在Update()方法,判断Content的位置是否超出界限。如果是,根据滚动的方向,调整Image1、Image2和Image3的位置,使它们循环滚动。 c. 检测Content的位置是否需要调整,以保持滚动的平滑性。 通过上述步骤和逻辑代码的实现,我们可以在Unity使用三个Image实现无限滚动的列表效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值