android 仿QQ五毛特效之查看红包领取详情界面

这篇博客介绍了如何在Android中仿照QQ的五毛特效,创建一个查看红包领取详情界面。通过自定义NotifyingScrollView监听滚动高度,并在MainActivity中设置回调,根据滚动位置动态改变顶部ImageView的高度,实现类似红包滑动展开的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

return width;

}

/**

  • 获得屏幕高度

*/

public static int getScreenHeight(Context context) {

WindowManager wm = (WindowManager) context

.getSystemService(Context.WINDOW_SERVICE);

DisplayMetrics outMetrics = new DisplayMetrics();

wm.getDefaultDisplay().getMetrics(outMetrics);

int height = outMetrics.heightPixels;

return height;

}

需要写一个自定义的ScrollView 来获取滚动的高度

import android.content.Context;

import android.util.AttributeSet; 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》无偿开源 徽信搜索公众号【编程进阶路】

import android.widget.ScrollView;

//重写ScrollView

public class NotifyingScrollView extends ScrollView {

/**

  • @author Cyril Mottier

*/

public interface OnScrollChangedListener {

void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt);

}

private OnScrollChangedListener mOnScrollChangedListener;

public NotifyingScrollView(Context context) {

super(context);

}

public NotifyingScrollView(Context context, AttributeSet attrs) {

super(context, attrs);

}

public NotifyingScrollView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}

/**

  • t 为目前滑动的高度

*/

@Override

protected void onScrollChanged(int l, int t, int oldl, int oldt) {

super.onScrollChanged(l, t, oldl, oldt);

if (mOnScrollChangedListener != null) {

mOnScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt);

}

}

public void setOnScrollChangedListener(OnScrollChangedListener listener) {

mOnScrollChangedListener = listener;

}

}

MainActivity代码

package com.yqy.cmd;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.View;

import android.view.ViewGroup.LayoutParams;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.RelativeLayout;

import android.widget.ScrollView;

import com.yqy.cmd.NotifyingScrollView.OnScrollChangedListener;

public class MainActivity extends Activity {

private ImageView topIv;

private NotifyingScrollView mySv;

private View view;

private LinearLayout myLl;

private int width;

private int height;

private int allowHeight;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

topIv = (ImageView) findViewById(R.id.topIv);

mySv = (NotifyingScrollView) findViewById(R.id.mySv);

view = findViewById(R.id.view);

myLl = (LinearLayout) findViewById(R.id.myLl);

//屏幕的宽高单位为px,所以我们在布局中需要转换为dp

width = ScreenUtils.getScreenWidth(this);

height = ScreenUtils.getScreenHeight(this);

allowHeight = Utils.px2dip(this, height) / 3;

topIv.setLayoutParams(new RelativeLayout.LayoutParams(

LayoutParams.MATCH_PARENT, Utils.px2dip(this, height) / 3));

view.setLayoutParams(new LinearLayout.LayoutParams(

LayoutParams.MATCH_PARENT, Utils.px2dip(this, height) / 3));

mySv.setOnScrollChangedListener(new OnScrollChangedListener() {

@Override

public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {

if(t < allowHeight){

int height = allowHeight - t;

topIv.setLayoutParams(new RelativeLayout.LayoutParams(height*3, height));

}

}

});

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

activity_main布局

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:background=“@android:color/holo_red_light”

tools:context=“.MainActivity”

tools:ignore=“NewApi” >

<ImageView

android:id=“@+id/topIv”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:background=“@drawable/ic_launcher” />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值