Android横向伸缩,Android 实现伸缩布局效果示例代码

最近项目实现下面的图示的效果,本来想用listview+gridview实现,但是貌似挺麻烦的于是就用flowlayout 来addview实现添加伸缩的效果,实现也比较简单。

3923ed3c47cdff30afec1ad401de1952.png

mainActivity 布局

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

>

android:id="@+id/rl_category_title_bar_layout"

android:layout_height="wrap_content"

android:layout_width="match_parent"

>

android:layout_height="50dp"

android:layout_width="match_parent"

>

android:id="@+id/tv_category_title"

android:layout_height="50dp"

android:layout_width="wrap_content"

android:text="分类"

android:textSize="18sp"

android:layout_centerInParent="true"

android:gravity="center"

/>

android:id="@+id/lv_category_menu"

android:layout_height="match_parent"

android:layout_width="match_parent"

/>

自定义布局flowlayout

package comskyball.addflowlayout;

import android.content.Context;

import android.content.res.TypedArray;

import android.util.AttributeSet;

import android.view.View;

import android.view.ViewGroup;

import java.util.ArrayList;

import java.util.List;

public class FlowLayout extends ViewGroup {

private Context mContext;

private int usefulWidth; // the space of a line we can use(line's width minus the sum of left and right padding

private int lineSpacing = 0; // the spacing between lines in flowlayout

List childList = new ArrayList();

List lineNumList = new ArrayList();

public FlowLayout(Context context) {

this(context, null);

}

public FlowLayout(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

mContext = context;

TypedArray mTypedArray = context.obtainStyledAttributes(attrs,

R.styleable.FlowLayout);

lineSpacing = mTypedArray.getDimensionPixelSize(

R.styleable.FlowLayout_lineSpacing, 0);

mTypedArray.recycle();

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int mPaddingLeft = getPaddingLeft();

int mPaddingRight = getPaddingRight();

int mPaddingTop = getPaddingTop();

int mPaddingBottom = getPaddingBottom();

int widthSize = MeasureSpec.getSize(widthMeasureSpec);

int heightMode = MeasureSpec.getMode(heightMeasureSpec);

int heightSize = MeasureSpec.getSize(heightMeasureSpec);

int lineUsed = mPaddingLeft + mPaddingRight;

int lineY = mPaddingTop;

int lineHeight = 0;

for (int i = 0; i < this.getChildCount(); i++) {

View child = this.getChildAt(i);

if (child.getVisibility() == GONE) {

continue;

}

int spaceWidth = 0;

int spaceHeight = 0;

LayoutParams childLp = child.getLayoutParams();

if (childLp instanceof MarginLayoutParams) {

measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, lineY);

MarginLayoutParams mlp = (MarginLayoutParams) childLp;

spaceWidth = mlp.leftMargin + mlp.rightMargin;

spaceHeight = mlp.topMargin + mlp.bottomMargin;

} else {

measureChild(child, widthMeasureSpec, heightMeasureSpec);

}

int childWidth = child.getMeasuredWidth();

int childHeight = child.getMeasuredHeight();

spaceWidth += childWidth;

spaceHeight += childHeight;

if (lineUsed + spaceWidth > widthSize) {

/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 横向扫描的动画可以通过使用 ViewPropertyAnimator 和 ObjectAnimator 来实现。 首先,在 XML 布局文件中创建一个 ImageView,并设置其宽度为 0dp,高度为 match_parent。然后,创建一个横向的渐变色 Drawable,并将其设置为 ImageView 的背景。最后,使用 ViewPropertyAnimator 来设置 ImageView 的宽度变化,实现横向扫描的动画效果。 具体实现步骤如下: 1. 在 XML 布局文件中添加 ImageView 和横向渐变色 Drawable: ``` <ImageView android:id="@+id/scan_line" android:layout_width="0dp" android:layout_height="match_parent" android:background="@drawable/scan_gradient" /> ``` 2. 创建一个横向渐变色 Drawable,命名为 scan_gradient.xml: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="#FFFFFFFF" android:endColor="#00FFFFFF" android:type="linear" android:angle="0"/> </shape> ``` 3. 使用 ViewPropertyAnimator 来设置 ImageView 的宽度变化: ``` // 获取 ImageView ImageView scanLine = findViewById(R.id.scan_line); // 获取屏幕宽度 int screenWidth = getResources().getDisplayMetrics().widthPixels; // 设置 ViewPropertyAnimator scanLine.animate() .translationX(screenWidth) // 横向移动到屏幕右侧 .setDuration(3000) // 动画时长为 3 秒 .setInterpolator(new LinearInterpolator()) // 设置动画插值器 .withEndAction(new Runnable() { @Override public void run() { // 动画结束后,重新设置 ImageView 的宽度为 0 scanLine.setTranslationX(0); scanLine.animate().setStartDelay(1000).start(); } }) .start(); ``` 上述代码中,使用 setDuration 方法设置动画时长为 3 秒,使用 setInterpolator 方法设置动画插值器为 LinearInterpolator,使得动画的速度保持匀速不变。使用 withEndAction 方法设置动画结束后的回调,重新设置 ImageView 的宽度为 0,并使用 setStartDelay 方法设置动画延迟 1 秒后再次启动,实现无限循环横向扫描的动画效果

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值