在写这边blog的时候都在考虑要不要写,因为关于PaintDrawable的东西,我知道的实在是太少了,而且还不太知道这玩意到底有什么用(能当做UI背景这种事我们就不提了,这种事情是个Drawable都可以干)。
我就直接代码吧,PaintDrawable无法通过xml创建,只能在java代码中创建。今天就用它设置一个UI的背景吧!
先看一下效果吧!
效果很简单,也很low,知道的就请直接飘过吧!
主布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.paintdrawable.MainActivity"> <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="400dp" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/iv" android:layout_marginTop="20dp" android:onClick="setImageViewBg" android:text="@string/set_bg" /> </RelativeLayout>java文件:
package com.example.paintdrawable; import android.graphics.Color; import android.graphics.drawable.PaintDrawable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private PaintDrawable bg_1; private PaintDrawable bg_2; private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView)findViewById(R.id.iv); bg_1 = new PaintDrawable(Color.BLUE); bg_1.setCornerRadius(200.0f); bg_2 = new PaintDrawable(Color.RED); bg_2.setCornerRadii(new float[]{100,200,100,200,200,400,200,400}); iv.setBackground(bg_1); } public void setImageViewBg(View view){ iv.setBackground(bg_2); } }到这里也基本结束了,主要还是我不知道,google设计这个类到底是出于什么样的目的的! 这是我的微信公众号,如果可以的话,还请您关注一下,这将是对我最大的鼓励了! 代码的地址在GitHub上!!!! 我一定是脑子秀逗了,才会把它归到Material Design仓库中
