文章目录
-
- 1. 绘制矩形、扇形、圆角矩形
- 2.用xml代码布局文件实现复杂的图像
- 3.圆环形渐变
- 4.矩形渐变
- 5.渐变线
- 6.动态渐变
- 7.对图片进行裁剪
- 8.灵活的调用图片在Imageview显示
- 9.红色小圆球样式的新消息提醒
- 10.线性梯度渐变渲染(LinearGradient)
- 11.扫描梯度渐变渲染器(SweepGradient)
- 12.放射环状梯度渐变渲染器(RadialGradient)
- 13.用shape文件实现线性梯度渐变渲染
- 14.用shape文件实现放射状梯度渐变渲染
- 15.用shape文件实现扫描形梯度渐变渲染
- 16.透明渐变属性动画
- 17.旋转属性动画
- 18.位移属性动画
- 19.拉申属性动画
- 20.酷炫的动画往往不止一个效果,而是同时,或是按照一定序列联合执行一组动画集。AnimatorSet(动画集)
- 21.拖曳管控
- 22.横竖屏切换不同尺寸的view
- 23.onMeasure控制view的尺寸大小
- 24.onLayout控制子View的空间位置
- 25.Android手机在状态栏的消息通知
1. 绘制矩形、扇形、圆角矩形
// activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="150dp"
android:layout_height="100dp"
android:id="@+id/textview1" />
<TextView
android:layout_width="150dp"
android:layout_height="100dp"
android:id="@+id/textview2"/>
<TextView
android:layout_width="150dp"
android:layout_height="100dp"
android:id="@+id/textview3"/>
<TextView
android:layout_width="150dp"
android:layout_height="100dp"
android:id="@+id/textview4"/>
</LinearLayout>
//main.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//圆角矩形
//PaintDrawable继承自ShapeDrawable
PaintDrawable drawable1=new PaintDrawable(Color.GREEN);
drawable1.setCornerRadius(60);//圆角弧度
findViewById(R.id.textview1).setBackgroundDrawable(drawable1);
//椭圆
OvalShape ovalShape = new OvalShape();
ShapeDrawable drawable2=new ShapeDrawable(ovalShape);
drawable2.getPaint().setColor(Color.BLUE);
drawable2.getPaint().setStyle(Paint.Style.FILL);//填充形式为完全填充
findViewById(R.id.textview2).setBackgroundDrawable(drawable2);
//矩形
RectShape rectShape =new RectShape();
ShapeDrawable drawable3=new ShapeDrawable(rectShape);
drawable3.getPaint().setColor(Color.RED);
drawable3.getPaint().setStyle(Paint.Style.FILL);//填充形式为完全填充
findViewById(R.id.textview3).setBackgroundDrawable(drawable3);
//弧度扇形
ArcShape arcShape = new ArcShape(100,120);//顺时针开始角度100°, 绘制区域为120°
ShapeDrawable drawable4=new ShapeDrawable(arcShape);
drawable4.getPaint().setColor(Color.BLACK);
drawable4.getPaint().setStyle(Paint.Style.FILL);//填充形式为完全填充
findViewById(R.id.textview4).setBackgroundDrawable(drawable4);
}
2.用xml代码布局文件实现复杂的图像
IDE: Android studio
【1】创建Shape文件:
(1) 右击app->New->Android resource file
(2) File name: 填写Shape文件的名字(如 gradient)
(3) Resource type: Drawable
(4) Root element: shape
(5) Shape文件中的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"> // 定义一个椭圆
<gradient
android:angle="45"
android:centerColor="@android:color/holo_orange_dark"
android:endColor="@android:color/holo_blue_dark"
android:startColor="@android:color/holo_red_dark" />
<stroke //描边
android:width="10dip" //所描边的厚度
android:color="@android:color/black"
android:dashGap="50dip" //生成不连续间断的长度
android:dashWidth="111dip" //黑色断片的长度
/>
</shape>
【2】在布局文件activity_main.xml中加入
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/image_view"
android:layout_centerInParent="true"
android:background="@drawable/gradient"
/>
</RelativeLayout>
【3】java代码中只需
setContentView(R.layout.activity_main);
3.圆环形渐变
shape文件代码如下(替换实例2的shap文件)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:thickness="20dp"
android:useLevel="false">
<gradient
android:angle="90"
android:centerColor="@android:color/holo_orange_dark"
android:endColor="@android:color/holo_blue_dark"
android:startColor="@android:color/holo_red_dark"/>
<stroke
android:width="5dp"
android:color="@android:color/black"/>
</shape>
4.矩形渐变
shape文件如下(替换实例2的shap文件)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="90"
android:centerColor="@android:color/holo_orange_dark"
android:endColor="@android:color/holo_blue_dark"
android:startColor="@android:color/holo_red_dark"/>
<stroke
android:width="5dp"
android:color="@android:color/black"/>
</shape>
5.渐变线
shape文件如下(替换实例2的shap文件)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="0"
android:centerColor="@android:color/holo_blue_light"
android:endColor="@android:color/transparent" //transparent为黑色全透明
android:startColor="@android:color/transparent"/>
<size android:height="1px"/>
</shape>
6.动态渐变
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Drawable[] drawables=new Drawable[]{new ColorDrawable(Color.TRANSPARENT),new ColorDrawable(Color.RED)};
TransitionDrawable mTransitionDrawable=new TransitionDrawable(drawables);
ImageView image=(ImageView)findViewById(R.id.image_view);
image.setImageDrawable(mTransitionDrawable);
mTransitionDrawable.setCrossFadeEnabled(true);
mTransitionDrawable.startTransition(10000);
}
7.对图片进行裁剪
#正常图像
ImageView image1=(ImageView) findViewById(R.id.image_view1);
image1.setImageResource(R.drawable.yes);
#矩形四角磨成圆脚
RoundedBitmapDrawable round= RoundedBitmapDrawableFactory.create(getResources()
, BitmapFactory.decodeResource(getResources(),R.drawable.yes));
round.getPaint().setAntiAlias(true);
round.setCornerRadius(30);
ImageView image2=(ImageView) findViewById(R.id.image_view2);
image2.setImageDrawable(round);
#裁剪为圆形图像
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.yes);
RoundedBitmapDrawable circle= RoundedBitmapDrawableFactory.create(getResources()
, BitmapFactory.decodeResource(getResources(),R.drawable.yes));
circle.getPaint().setAntiAlias(true);
circle.setCornerRadius(Math.max(bitmap.getWidth(),bitmap.getHeight()));
ImageView image3=(ImageView) findViewById(R.id.image_view3);
image3.setImageDrawable(circle);
8.灵活的调用图片在Imageview显示
p.xml(在drawable中)代码如下:
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="1" android:drawable="@drawable/a"/>
<item android:maxLevel="2" android:drawable="@drawable/b"/>
<item android:maxLevel="3" android:drawable="@drawable/c"/>
</level-list>
*.java
ImageView image1=(ImageView) findViewById(R.id.image_view1);
image1.setImageResource(R.drawable.p);
image1.setImageLevel(3);
9.红色小圆球样式的新消息提醒
*.java
setContentView(R.layout.activity_main);
shape文件(round.xml)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--填充颜色值-->
<solid android:color="#FFA500"/>
<!--radius值越大,越趋于圆形-->
<corners android:radius="5dip" />
<!--圆角图像内部填充四周的大小,将会已此挤压内部布置的view-->
<padding
android:bottom="3dip"
android:left="3dip"
android:right="3dip"
android:top="3dip"/>
</shape>
shape文件(tips_circle.xml)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:useLevel="false">
<solid android:color="#FF0000"/>
</shape>
布局文件
<?xml version="1.0" encoding="utf-8"?>
<!--(帧布局)
android:layout_m