手机助手(三):自定义属性 + 飞入飞出

2024-01-28 21:32

本文主要是介绍手机助手(三):自定义属性 + 飞入飞出,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简介

今天主要完成专题和推荐模块。专题是用listview显示一组图片,推荐是飞入飞出的效果。

效果图

这里写图片描述

怎么给轮播图设置点击事件 ?

昨天添加了循环滚动的轮播图,但是没有添加点击事件,我们平时看到的app都是点击轮播图然后跳转到新界面,其实就是给轮播图的ImageView添加点击事件而已。

效果图

这里写图片描述

代码:轮播图中的ViewPager的adapter

public class BannerPagerAdapter extends BasePagerAdapter<String> {public BannerPagerAdapter(List<String> list) {super(list);}@Overridepublic Object instantiateItem(ViewGroup container, final int position) {ImageView iv = new ImageView(container.getContext());ImageLoader.getInstance().displayImage(NetUrl.IMAGE_PREFIX + list.get(position), iv, ImageLoaderOptions.pagerOptions);LayoutParams params = new LayoutParams();params.width = LayoutParams.MATCH_PARENT;params.height = LayoutParams.MATCH_PARENT;container.addView(iv,params);//给imageview添加点击事件iv.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {ToastUtil.showShortToast("position="+position+"url = "+NetUrl.IMAGE_PREFIX + list.get(position));}});return iv;}
}

自定义属性

参考:自定义控件:自定义组合控件+自定义属性

第一步:在attrs.xml中声明自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="RatioImageView"><attr name="ratio" format="float" /></declare-styleable></resources>

第二步:使用自定义属性

这里写图片描述

第三步:从命名空间中获取自定义的属性

//都可以
//      String namespace = "http://schemas.android.com/apk/res/ratioimageview";
String namespace = "http://schemas.android.com/apk/res/com.cqc.googleplay";
ratio = attrs.getAttributeFloatValue(namespace, "ratio", 2.42f);//获取xml中的自定义属性的值

RatioImageView:动态设置图片的比

学会了自定义属性,我们才可以在RatioImageView使用它

效果图

这里写图片描述
这里写图片描述

代码

public class RatioImageView extends ImageView {private float ratio =0f;//图片的宽高比private int makeMeasureSpec;public RatioImageView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public RatioImageView(Context context, AttributeSet attrs) {super(context, attrs);String namespace = "http://schemas.android.com/apk/res/ratioimageview";ratio = attrs.getAttributeFloatValue(namespace, "ratio", 2.42f);//获取xml中的自定义属性的值}public RatioImageView(Context context) {super(context);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int width = MeasureSpec.getSize(widthMeasureSpec);if (ratio != 0) {float height = width / ratio;heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) height, MeasureSpec.EXACTLY);} super.onMeasure(widthMeasureSpec, heightMeasureSpec);}
}

SubjectFrag.java和AppFrag差不多,不再展示。

推荐:

参考:自定义控件:飞入飞出的效果
需要导入4个类
这里写图片描述

public class RecommendFrag extends Fragment {private static final String TAG = "RecommendFrag";private ContentPager contentPager;private List<String> list = new ArrayList<String>();private StellarMapAdapter adapter;private int padding = 10;private StellarMap map;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {contentPager = new ContentPager(getActivity()) {@Overridepublic Object requestData() {return getData();}@Overridepublic View createSuccessView() {return getSuccessView();}};return contentPager;}protected View getSuccessView() {int padding = (int) CommonUtil.getDimension(R.dimen.recommend_frag_padding);map = new StellarMap(getActivity());map.setInnerPadding(padding, padding, padding, padding);return map;}protected Object getData() {String json = HttpHelper.get(NetUrl.RECOMMEMD);List textList = (List<String>) GsonUtil.jsonToList(json, new TypeToken<List<String>>() {}.getType());list.addAll(textList);CommonUtil.runOnUIThread(new Runnable() {@Overridepublic void run() {adapter = new  StellarMapAdapter(list, getActivity());  map.setAdapter(adapter);map.setGroup(0, true); //默认显示第几组数据map.setRegularity(15, 15); //X Y方向上的密度          }});return json;}
}

怎么随机设置TextView的字体大小和颜色?

这里写图片描述

Random random = new Random();
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, random.nextInt(5)+15);//[15,20)int red = random.nextInt(150)+50;//[150,200)
int green = random.nextInt(150)+50;//[150,200)
int blue = random.nextInt(150)+50;//[150,200)
int textColor = Color.rgb(red, green, blue);
tv.setTextColor(textColor);

源码

https://git.oschina.net/googlepalycqc/GooglePlayCQCDay03

这篇关于手机助手(三):自定义属性 + 飞入飞出的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/654825

相关文章

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor

SpringBoot读取ZooKeeper(ZK)属性的方法实现

《SpringBoot读取ZooKeeper(ZK)属性的方法实现》本文主要介绍了SpringBoot读取ZooKeeper(ZK)属性的方法实现,强调使用@ConfigurationProperti... 目录1. 在配置文件中定义 ZK 属性application.propertiesapplicati

Java反射实现多属性去重与分组功能

《Java反射实现多属性去重与分组功能》在Java开发中,​​List是一种非常常用的数据结构,通常我们会遇到这样的问题:如何处理​​List​​​中的相同字段?无论是去重还是分组,合理的操作可以提高... 目录一、开发环境与基础组件准备1.环境配置:2. 代码结构说明:二、基础反射工具:BeanUtils