一个项目玩转 Android 自定义 Drawable。

DreamDrawable

项目地址:yanbober/DreamDrawable 

简介: 一个项目玩转 Android 自定义 Drawable。

更多:作者   提 Bug   

标签:

 

一个项目玩转 Android 自定义 Drawable,具体原理参见《Android 应用层开发 Drawable 的一些叨叨絮》,效果如下图所示。

说明文档

当前实现了多个 Customer Drawable,依据自己情况使用其一即可,互相无任何依赖。

类型定义说明
RoundDrawableRoundDrawable圆角 Drawable,可实现类似上图中第一行各种效果。
方法RoundDrawable(Bitmap bitmap)构造方法直接传递一个 Bitmap 即可,然后使用即可,譬如 ImageView.setImageDrawable(drawable);
ReflectionDrawableReflectionDrawable底部具备倒影效果的 Drawable,可以实现类似上图中第二行各种效果等。
方法ReflectionDrawable(Bitmap bitmap)构造方法直接传递一个 Bitmap 即可,然后使用即可,譬如 ImageView.setImageDrawable(drawable);
方法setReflectionHeight(@IntRange(from = 0) int height)给 ReflectionDrawable 设置底部反转倒影的高度。
LauncherIconDrawableLauncherIconDrawable具备进度刷新效果的 Drawable,譬如 miui 桌面 app 更新图标进度,类似上图第三行等效果,还可配置。譬如 ImageView.setImageDrawable(drawable);
方法LauncherIconDrawable(Drawable drawable)构造方法传递一个 Drawable,譬如传递 ImageView.getDrawable();
方法setDefaultColor(@ColorInt int defaultColor)设置 LauncherIconDrawable 中图片默认的覆盖颜色,模式为 PorterDuff.Mode.MULTIPLY。
方法setDefaultColor(@ColorInt int defaultColor, PorterDuff.Mode mode)同上,设置 LauncherIconDrawable 中图片默认的覆盖颜色。
方法setPercentColor(@ColorInt int percentColor)设置 LauncherIconDrawable 中进度的覆盖颜色,模式为 PorterDuff.Mode.MULTIPLY。
方法setPercentColor(@ColorInt int percentColor, PorterDuff.Mode mode)同上,设置 LauncherIconDrawable 中进度的覆盖颜色。
方法setCurPercent(@FloatRange(from = 0f, to = 1f) float curPercent)设置当前进度为多少,类似 miui App 更新桌面图标进度更新。
IconViewIconView类似 Flyme 6.0 联系人 Icon 的 Drawable 的 View,可以实现类似上图中最后一行的各种效果,配合下面的 IconDrawable 使用。
方法IconDrawable getIconDrawable()获取 IconView 中的 IconDrawable 来使用。
IconDrawableIconDrawable配合上面 IconView 内部使用的,不用自己 new,只用通过 IconView 的 IconDrawable getIconDrawable() 获取来操作。
方法setTextLabel(String str)设置文字,譬如联系人姓或者首字母等。
方法setTextFontSize(int size)设置文字大小,不设置默认会自适应。
方法setTextColor(int color)设置文字颜色。
方法setBackgroundColor(int color)设置为文字时的背景颜色。
方法setIconLabel(Bitmap bitmap)设置为联系人默认或者真是头像。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Android 自定义列表适配器的示例代码: 1. 首先,在布局文件中定义列表项的布局,例如 list_item.xml: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <ImageView android:id="@+id/item_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/item_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="16dp" android:textSize="16sp" android:textColor="#000000" android:text="Item Name" /> </LinearLayout> ``` 2. 创建一个继承自 BaseAdapter 的自定义适配器类,例如 MyListAdapter: ```java public class MyListAdapter extends BaseAdapter { private Context mContext; private List<MyListItem> mItems; public MyListAdapter(Context context, List<MyListItem> items) { mContext = context; mItems = items; } @Override public int getCount() { return mItems.size(); } @Override public Object getItem(int position) { return mItems.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = LayoutInflater.from(mContext); View itemView = convertView; if (itemView == null) { itemView = inflater.inflate(R.layout.list_item, parent, false); } MyListItem item = mItems.get(position); ImageView imageView = (ImageView) itemView.findViewById(R.id.item_image); imageView.setImageResource(item.getImageResId()); TextView textView = (TextView) itemView.findViewById(R.id.item_text); textView.setText(item.getText()); return itemView; } } ``` 3. 在 Activity 中使用自定义适配器,例如 MainActivity: ```java public class MainActivity extends AppCompatActivity { private ListView mListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.list_view); List<MyListItem> items = new ArrayList<>(); items.add(new MyListItem(R.drawable.item1, "Item 1")); items.add(new MyListItem(R.drawable.item2, "Item 2")); items.add(new MyListItem(R.drawable.item3, "Item 3")); MyListAdapter adapter = new MyListAdapter(this, items); mListView.setAdapter(adapter); } private static class MyListItem { private int imageResId; private String text; public MyListItem(int imageResId, String text) { this.imageResId = imageResId; this.text = text; } public int getImageResId() { return imageResId; } public String getText() { return text; } } } ``` 这样,就可以自定义一个简单的列表适配器,并在 Activity 中使用它来显示列表项。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值