Android实战绘述01-一个小Demo

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.数据的保存

<!--android:visibility="gone",隐藏控件,不占用空间。
    invisible,隐藏空间,占用空间。可以利用这个特性保存数据。-->
<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"/>
    
   <!--隐藏页面必须添加android:layout_gravity="start"-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:layout_marginEnd="-65dp"/>
    <!--android:layout_marginEnd="-65dp",当左边的页面出来的时候,
    不能全部占满屏幕,会距离屏幕右边框65dp,所以我们设置layout_marginEnd=
    -65,保证其充满屏幕-->
</androidx.drawerlayout.widget.DrawerLayout>
 final DrawerLayout drawerLayout = findViewById(R.id.drawer);
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
    /**
             * 设置左边抽屉移出的效果
             * 左边和主窗口一起往右边移动
             * @param drawerView
             * @param slideOffset
             */
    @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=""/>
<!--app:menu="",可以直接引入菜单项,
    app:headerLayout="",可以直接映入一个@layout的布局-->

implementation 'com.android.support:design:29.0.0'

7.后期加强-关于悬浮

<!--使用FloatingActionButton,自动悬浮-->
<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

//Toast只是显示一个提示,用户不能进行操作;Snackbar可以和用户进行操作。
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">

        <!--AppBarLayout内容封装多个滚动事件,可以解决RecyclerView遮挡Toolbar的问题
        解决的原理就是,RecyclerView滚动的时候将滚动事件通知给AppBarLayout,AppBarLayout
        收到通知,内部的子控件可以去处理这些事件
        app:layout_scrollFlags="scroll|enterAlways|snap",其中scroll表示,当RecyclerView
        向上滚动的时候,Toolbar一起向上滚动并实现隐藏;enterAlways,表示向下滚动的时候Toolbar也向下滚动
        并重新显示;snap表示可以根据当前距离自动选择或者隐藏-->
        <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() {
                    //init(); 重新生数据
                    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);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值