1.制作圆形图片
public Bitmap makeRoundCorner(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int left = 0, top = 0, right = width, bottom = height;
float roundPx = height/2;
if (width > height) {
left = (width - height)/2;
top = 0;
right = left + height;
bottom = height;
} else if (height > width) {
left = 0;
top = (height - width)/2;
right = width;
bottom = top + width;
roundPx = width/2;
}
Log.i("LogKK",+ left + top +right +", "+ bottom);
Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
int color = 0xff424242;
Paint paint = new Paint();
Rect rect = new Rect(left, top, right, bottom);
RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
2.数据库升级
public void onUpgrede(SQLiteDatebase sqLiteDatebase,int i,int i1){
sqLiteDatebase.execSQL("drop table if exists user");
onCreate(sqLiteDatebase);
}
3.数据的保存
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
4.抽屉侧拉
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginEnd="-65dp"/>
</androidx.drawerlayout.widget.DrawerLayout>
final DrawerLayout drawerLayout = findViewById(R.id.drawer);
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
int right = drawerLayout.getRight();
int mWidth = 0;
int mHeight = 0;
drawerLayout.layout(right,0,right + mWidth,mHeight);
}
@Override
public void onDrawerOpened(@NonNull View drawerView) {
}
@Override
public void onDrawerClosed(@NonNull View drawerView) {
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
5.后期加强-圆形图片的处理
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/wel_img1"/>
implementation 'de.hdodenhof:circleimageview:3.0.1'
6.后期加强-抽屉新实现
<com.google.android.material.navigation.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu=""
app:headerLayout=""/>
implementation 'com.android.support:design:29.0.0'
7.后期加强-关于悬浮
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="20dp"
android:src="@mipmap/ic_launcher"/>
8.后期加强-关于Toast
FloatingActionButton button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v,"Data delete",Snackbar.LENGTH_SHORT)
.setAction("Undo", new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"Data restart",Toast.LENGTH_SHORT).show();
}
}).show();
}
});
9.后期加强-关于FrameLayout和CoordinatorLayout
可以选择使用CoordinatorLayout替换FrameLayout,CoordinatorLayout功能更加的强大,可以监听所有子空间的事件,然后帮助我们做出合理的相应,相应滚动事件。
10.后期加强-RecyclerView和Toolbar的联动
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|snap"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
11.后期加强-RecyclerView数据刷新
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
SwipeRefreshLayout srl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
srl = findViewById(R.id.refresh);
srl.setColorSchemeResources(R.color.colorPrimary);
srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refresh();
}
});
}
private void refresh(){
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
}catch (Exception e){
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChange();
srl.setRefreshing(false);
}
});
}
}).start();
}
12.其他的控件
1:NestedScrollView允许使用滚动事件查看,屏幕以外的数据,而且还可以嵌套滚动事件。
2:CollapsingToolbarLayout,一个折叠式的Toolbar。
13.细节用法
1:NestedScrollView和ScrollView内部只允许一个直接子布局。
2:CoordinatorLayout -> AppBarLayout -> CollapsingToolbarLayout。
14.后期加强-日志封装
public class LogUtils {
public static boolean isOutput = true;
private final static int VERBOSE = 1;
private final static int DEBUG = 2;
private final static int INFO = 3;
private final static int WARE = 4;
private final static int ERROR = 5;
public static void v(String tag,String msg){
if (isOutput){
Log.v(tag,msg);
}
}
public static void d(String tag,String msg){
if (isOutput){
Log.d(tag,msg);
}
}
public static void i(String tag,String msg){
if (isOutput){
Log.i(tag,msg);
}
}
public static void w(String tag,String msg){
if (isOutput){
Log.w(tag,msg);
}
}
public static void e(String tag,String msg){
if (isOutput){
Log.e(tag,msg);
}
}
}