1. 功能介绍
基础Api:
Drawable(可绘制对象), 可以对应一张位图(BitmapDrawable) 图形(ShapeDrawable) 图层(LayerDrawable)
Bitmap (位图),对应内存中的.bmp,.jpg,.png
Canvas: 画布
Paint: 画笔,设置字体大小、颜色、样式
Matrix: 矩阵, 用于图形平面缩放、平移、旋转等操作
2. 图片读取与保存
package mk.denganzhi.com.shapemodel;
public class MainActivity extends AppCompatActivity {
ImageView myImg;
public static int MY_PERMISSIONS_REQUEST_CALL_PHONE=100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImg = findViewById(R.id.myImg);
// 1. 加载资源下图片
myImg.setImageResource(R.mipmap.ic_launcher);
// 2. 加载sd卡上图片
writeSDW();
}
// 写入SD卡内容
public void writeSDW() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},MY_PERMISSIONS_REQUEST_CALL_PHONE);
} else
{
loadImg();
}
}
public void loadImg(){
String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
// 加载到内存中,sd卡根目录下图片
Bitmap bitmap = BitmapFactory.decodeFile(sdPath+"/aa.png");
myImg.setImageBitmap(bitmap);
// 写入到file下
try {
bitmap.compress(Bitmap.CompressFormat.PNG,100,openFileOutput("aa.png", Context.MODE_PRIVATE));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE)
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
loadImg();
} else
{
// Permission Denied
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
return;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
图片兼容Api: Drawable drawable=ContextCompat.getDrawable(this, R.mipmap.ws130_lock_type);
Android Drawable资源 API :
1、 图片 资源 png、jpg、 gif
2、 StateListDrawable 资源
可以组织 多个Drawable, StateListDrawable 有多个item, 每个item支持不同状态
android:state_active 激活状态
android:state_press 按下
android:checkable: 可勾选状态
android:checked: 已勾选状态
android:enable 可用状态
android:select 选择状态
android:foucused="true|false" 获取焦点|失去焦点
ShapeDrawable: 表示一个几何图形LayerDrawable
ClipDrawable
AnimationDrawable: 动画
3. shape使用, 设置按钮圆角、描边
目录 在 res/drawable/myshape.xml下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<!-- 指定形状: “rectangle” | “oval” | “line” | “ring” -->
<!-- 默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring) -->
<!-- 圆角 radius半径 -->
<corners android:radius="8dp"></corners>
<!-- 描边(边框)
color 颜色
width 边框宽度
dashWidth: 一个虚线长度
dashGap: 虚线间隔
-->
<stroke android:color="@color/colorPrimary"
android:width="1dp"
android:dashWidth="3dp"
android:dashGap="2dp"></stroke>
<!-- 默认尺寸(wrap_content时)
width 宽度
height 高度
-->
<size android:width="50dp"
android:height="50dp"></size>
<!-- 内边框: 指定上下左右的距离 -->
<padding android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"></padding>
<!-- 内部单色填充: color: 颜色-->
<solid android:color="#00ff00" ></solid>
<!-- 内部渐变色填充
startColor: 开始颜色值
centerColor: 中间颜色值
endColor: 结束颜色值
angle: 色变的方向
-->
<gradient android:startColor="#ffffff"
android:endColor="#ffffff"
android:centerColor="#ff88c0"
android:angle="0"></gradient>
</shape>
如何使用:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="40dp"
android:text="Button"
android:background="@drawable/myshape"/>
效果:
4. Selector+Drawable 设置按钮点击切换不同图片 myselect.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- selector多状态图形,可以在按下、选中选中不同图形 -->
<!-- 按下的图片,松开变成正常,按下去找第一个,所有这个必须写在第一个 -->
<item android:state_pressed="true" android:drawable="@drawable/main_index_search_pressed"> </item>
<item android:drawable="@drawable/main_index_search_normal"></item>
</selector>
5. Selector+Shape 设置按钮点击切换不同Shape 比如按下切换颜色 myselect_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<corners android:radius="5dp"></corners>
<solid android:color="#ff0000"></solid>
</shape>
</item>
<item >
<shape>
<corners android:radius="5dp"></corners>
<solid android:color="#00ff00"></solid>
</shape>
</item>
</selector>
5. Maxtrix矩阵使用:
xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.denganzhi.myapplication.MainActivity">
<!-- 点击按钮 -->
<Button
android:id="@+id/scaleBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放"/>
<!-- ImageView必须要设置 android:scaleType="matrix" matrix中的api才起作用 -->
<ImageView
android:id="@+id/scaleImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:scaleType="matrix"/>
</LinearLayout>
代码实现:
package com.denganzhi.myapplication;
import android.graphics.Matrix;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
Button scaleBtn;
ImageView scaleImg;
Matrix matrix;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scaleBtn = (Button)findViewById(R.id.scaleBtn);
scaleImg = (ImageView)findViewById(R.id.scaleImg);
matrix=new Matrix();
scaleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// matrix 操作的是图片的图层,不操作控件
// matrix放大,图片会超过内容区域,在图层区域缩放,控件部分不变
matrix.postScale(1.1f,1.1f); // 缩放
// matrix.postRotate(10.0f); // 旋转
// matrix.postTranslate(10,10); //平移
// matrix.reset(); // 还原
scaleImg.setImageMatrix(matrix);
}
});
}
}
效果图:
6. 消息通知小功能实现:
<RelativeLayout
android:id="@+id/rl_comment_area"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pl_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:background="#bfa">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_comment_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="35dp"
android:background="@drawable/shape_red_comment_bg_notification"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="999"
android:textColor="@android:color/white"
android:textSize="12sp" />
</RelativeLayout>
shape_red_comment_bg_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="8dp"/>
<solid android:color="@color/common_red"/>
</shape>
效果图: