Android仿淘宝商品详情页
作者:AnalyzeSystem 时间:2023-09-08 18:37:26
看到有人在问如何实现淘宝商品详情页效果,献上效果图
大致梳理一下思路,这里不提供源码
状态栏透明使用开源库StatusBarCompat,为了兼容手机4.4
dependencies {
compile ('com.github.niorgai:StatusBarCompat:2.1.4', {
exclude group: 'com.android.support'
})
}
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
标题栏图标透明度变化参考Api setAlpha()已过时
icon.setImageAlpha(0);
Banner控件为ViewPager,淘宝显示为正方形,这里需要修改ViewPager measure函数
public class IdeaViewPager extends ViewPager {
private Point point;
public IdeaViewPager(Context context) {
this(context,null);
}
public IdeaViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
point = new Point();
windowManager.getDefaultDisplay().getSize(point);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(point.x,point.x);
}
}
测量View高度,获取到高度集合绑定到ScrollView,根据ScrollView滑动距离判断是属于哪一个Tab选项
public int getMeasureHeight(View view){
int width = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
int height = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
view.measure(width, height);
return view.getMeasuredHeight();
}
重新onScrollChanged函数,实现ViewPager滑动速度比其他View慢
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (viewPager != null && t != oldt) {
viewPager.setTranslationY(t/2);
}
}
根据限定距离(Banner)计算百分比偏移量,实现颜色渐变、透明度渐变(淘宝商品详情页有二次颜色渐变)
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if(viewPager!=null&&t<=point.x-headerHeight&&getOnScrollChangedColorListener()!=null){
getOnScrollChangedColorListener().onChanged(Math.abs(t)/Float.valueOf(point.x-headerHeight));
if(t<=(point.x-headerHeight)/2){
getOnScrollChangedColorListener().onChangedFirstColor(t/(point.x-headerHeight)/2);
}else{
getOnScrollChangedColorListener().onChangedSecondColor((t-(point.x-headerHeight)/2)/(point.x-headerHeight)/2);
}
}
int currentPosition = getCurrentPosition(t,arrayDistance);
if(currentPosition!=position&&getOnSelectedIndicateChangedListener()!=null){
getOnSelectedIndicateChangedListener().onSelectedChanged(currentPosition);
}
this.position = currentPosition;
}
单一颜色渐变透明度,还原argb通道,修改a值
ideaScrollView.setOnScrollChangedColorListener(new IdeaScrollView.OnScrollChangedColorListener() {
@Override
public void onChanged(float percentage) {
int color = getAlphaColor(percentage>0.9f?1.0f:percentage);
header.setBackgroundDrawable(new ColorDrawable(color));
radioGroup.setBackgroundDrawable(new ColorDrawable(color));
icon.setImageAlpha((int) ((percentage>0.9f?1.0f:percentage)*255));
radioGroup.setAlpha((percentage>0.9f?1.0f:percentage)*255);
setRadioButtonTextColor(percentage);
}
@Override
public void onChangedFirstColor(float percentage) {
}
@Override
public void onChangedSecondColor(float percentage) {
}
});
ideaScrollView.setOnSelectedIndicateChangedListener(new IdeaScrollView.OnSelectedIndicateChangedListener() {
@Override
public void onSelectedChanged(int position) {
isNeedScrollTo = false;
radioGroup.check(radioGroup.getChildAt(position).getId());
isNeedScrollTo = true;
}
});
public int getAlphaColor(float f){
return Color.argb((int) (f*255),0x09,0xc1,0xf4);
}
Tab选项属性不能太频繁,会有颜色值闪烁情况出现,这里需要策略
public void setRadioButtonTextColor(float percentage){
if(Math.abs(percentage-currentPercentage)>=0.1f){
for(int i=0;i<radioGroup.getChildCount();i++){
RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
radioButton.setTextColor(radioButton.isChecked()?getRadioCheckedAlphaColor(percentage):getRadioAlphaColor(percentage));
}
this.currentPercentage = percentage;
}
}
判断当前属于哪个选项,根据滑动距离与传入绑定的View高度集合来计算
private int getCurrentPosition(int t, ArrayList<Integer> arrayDistance) {
int index = 0;
for (int i=0;i<arrayDistance.size();i++){
if(i==arrayDistance.size()-1){
index = i;
}else {
if(t>=arrayDistance.get(i)&&t<arrayDistance.get(i+1)){
index = i;
break;
}
}
}
return index;
}
切换选项卡以及回到顶部按钮的具体实现参考scrollTo函数
private void scrollToPosition(int position){
scrollTo(0,arrayDistance.get(position));
}
以上代码实现了上图效果,当然也可以使用RecyclerView AbsListView做容器。
希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
来源:https://blog.csdn.net/AnalyzeSystem/article/details/79442196?utm_source=blogxgwz4
标签:Android,淘宝,详情页
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#获取Windows进程监听的TCP/UDP端口实例
2021-11-20 13:06:21
基于Flutter制作一个吃豆人加载动画
2022-12-14 02:55:46
![](https://img.aspxhome.com/file/2023/0/139040_0s.png)
MyBatis绑定错误提示BindingException:Invalid bound statement (not found)的解决方法
2021-10-22 03:58:44
Java Object定义三个点实现代码
2023-11-21 06:05:07
图文详解Java中的字节输入与输出流
2022-12-23 23:14:48
![](https://img.aspxhome.com/file/2023/4/81884_0s.jpg)
SpringBoot整合websocket实现即时通信聊天
2022-12-12 15:14:32
![](https://img.aspxhome.com/file/2023/7/78867_0s.jpg)
Android 自定义输入支付密码的软键盘实例代码
2021-08-09 11:41:43
![](https://img.aspxhome.com/file/2023/9/99789_0s.png)
Android开发ListView中下拉刷新上拉加载及带列的横向滚动实现方法
2023-06-16 01:09:52
![](https://img.aspxhome.com/file/2023/6/122126_0s.png)
Android编程中出现The connection to adb is down问题的解决方法
2022-06-27 17:46:07
.NET Core使用C#扫描并读取图片中的文字
2022-03-09 11:06:54
![](https://img.aspxhome.com/file/2023/6/111096_0s.jpg)
浅谈spring的重试机制无效@Retryable@EnableRetry
2021-12-12 10:42:57
![](https://img.aspxhome.com/file/2023/5/62165_0s.jpg)
Spring中集成Groovy的四种方式(小结)
2023-07-11 16:36:53
![](https://img.aspxhome.com/file/2023/7/73117_0s.jpg)
5种方法完美解决android软键盘挡住输入框方法详解
2021-10-31 17:31:38
![](https://img.aspxhome.com/file/2023/1/137781_0s.png)
Java Gradle项目中的资源正确获取方式
2022-10-05 09:00:50
![](https://img.aspxhome.com/file/2023/8/70568_0s.jpg)
Android自定义控件属性详细介绍
2022-09-03 09:47:02
Java枚举学习之定义和基本特性详解
2022-07-23 20:29:44
![](https://img.aspxhome.com/file/2023/5/64525_0s.png)
java实现快速打字游戏
2022-11-05 11:16:38
利用Flutter实现“孔雀开屏”的动画效果
2021-11-04 21:24:17
![](https://img.aspxhome.com/file/2023/0/138180_0s.gif)
浅析Spring Boot中的spring-boot-load模块
2023-11-23 02:39:31
![](https://img.aspxhome.com/file/2023/6/58756_0s.png)
fastjson转换对象实体@JsonProperty不生效问题及解决
2023-10-07 00:13:51
![](https://img.aspxhome.com/file/2023/2/65312_0s.png)