安卓开发-基础知识补习13

听说点赞关注的人,身体健康,万事如意,工作顺利,爱情甜蜜,一夜暴富,升职加薪……最终迎娶白富美!!!


‼️微信公众号:炜煜工作室

🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱

📡 :安卓开发-基础知识补习13💯

📰内容简介:

本文介绍了Context 和动画的一部分内容给,中间记录了一些源码,使用java代码进行开发,如果有问题的地方请不吝指教,如果对文内内容有不理解的地方,也希望能积极主动的联系博主进行深刻的探讨,以便于让博主更深刻的记住这篇博文的内容,好让博主在发光发热的道路上越走越远。[手动狗头]🐶努力,奋斗!

📎 标签:安卓;java;HttpURLConnection;

🔍一、Context 上下文

  1. Context:上下文、语境、环境。安卓系统中,可以理解为当前对象在应用程序中所处的工作环境。其内部定义很多访问应用程序环境中全局信息的接口,通过它可以访问到应用程序的资源有关的类。如:Resoutces,AssetManager,Package及权限。

  2. Context 提供了关于应用环境全局信息的接口。它是一个抽象类,它的执行被 Android 系统所提供。它允许获取以应用为特征的资源和类型,是一个统领一些资源(应用程序环境变量等)的上下文。

  3. [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mpelJC9O-1667989062730)(C:\Users\29933\AppData\Roaming\Typora\typora-user-images\image-20221108100303661.png)]

  4. Context:是一个抽象类,定义一系列与系统交互的接口。

  5. ContextWrapper:继承自Context类,是Context类的包装类(装饰器模式),内部维护一个Context类型的成员变量,mBase指向一个ContextImpl对象,ContextWrapper里面的方法调用时通过mBase来调用ContextImpl里面的方法,这里用到了代理模式。

  6. ContextImpl:继承自Context类,实现了Context类中抽象方法,时Context类的具体实现类。它我iActivity及其应用组件提供行下文环境,应用中使用到的Context的方法就是其实现的。

  7. ContextThemeWrapper:继承自ContextWrapper类,在ContextWrappwe的基础上增加与主题Theme相关的逻辑,即可以指定Theme的Context包装类,用于在View构造时为其提供Theme属性集。

    ContextImpl实现类中设计的主要核心类是:ActivityThreadLoadedApkPackageManagerResourcesManager,这几个类都是单例的,一个应用程序进程中是共用同一个对象的。
    ContextImpl是一种轻量级类,而LoadeApk是一个重量级类,ContextImpl中的大多数及逆行包操作的重量级函数世界上都是转向了LoadedApk对象相应的方法。
    Activity继承自ContextThremeWrapperApplicationSevice继承自ContextWrapper,他们直接或间接继承自ContextWrapper类,因此也拥有了一个Context类型的成员变量mBase指向一个ContextImpl对象,ContextImplContext类的具体实现类,所以也都拥有了Context提供的所有功能。
    代理模式:属于结构模式,也指为其他对象提供一种个代理以控制对这个对象的访问,代理模式又分为静态代理和动态代理,动态代理包含CGLib代理。
    装饰器模式:又叫做包装模式,也是结构性模式,是指在不改变现有对象结构的情况下下,动态的给该对象增加一些职责(即增加其额外的功能)的模式。
    
  8. Context的作用域

    Context作用域AppliationActivityService
    Show a DialogNoYESNO
    Start an Activity不推荐YES不推荐
    Layout Inflation不推荐YES不推荐
    Start a ServiceYESYESYES
    Send a BroadcastYESYSEYES
    Register Broadcast ReceicerYESYESYES
    Load Resource ValuesYESYESYES

    注意:需要加载布局显示界面的,尽可能使用Activity作为Context域,虽然加载布局,启动Activity可以使用Application和Service作为Context域,但是不推近啊,以免报错或UI莫名的使用系统默认的主题Theme来展示。

🔍二、一些源码

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/view01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:width="320dp"
            android:height="320dp"
            android:background="#f00"></TextView>
        <TextView
            android:id="@+id/view02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:width="280dp"
            android:height="280dp"
            android:background="#0f0"></TextView>
        <TextView
            android:id="@+id/view03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:width="240dp"
            android:height="240dp"
            android:background="#00f"></TextView>
        <TextView
            android:id="@+id/view04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:width="200dp"
            android:height="200dp"
            android:background="#ff0"></TextView>
        <TextView
            android:id="@+id/view05"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:width="160dp"
            android:height="160dp"
            android:background="#f0f"></TextView>
        <TextView
            android:id="@+id/view06"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:width="120dp"
            android:height="120dp"
            android:background="#0ff"></TextView>
    </FrameLayout>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HS5olUmg-1667989062732)(C:\Users\29933\AppData\Roaming\Typora\typora-user-images\image-20221108150333212.png)]

通过java代码来控制布局的显示方式,简单的通过一个ToggleButton或者Switch来控制,下面是简单的实现源码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity9">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <ToggleButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/toggle"
            android:textOff="横向排列"
            android:textOn="纵向排列"
            android:checked="true"></ToggleButton>
        <Switch
            android:id="@+id/switcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:textOff="横向排列"
            android:textOn="纵向排列"
            android:thumb="@drawable/check"></Switch>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/test">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="button1">
            </Button>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="button1">
            </Button>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="button1">
            </Button>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="button1">
            </Button>
        </LinearLayout>
        <AnalogClock
            android:layout_width="100dp"
            android:layout_height="100dp"></AnalogClock>
        <TextClock
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:textColor="#f0f"
            android:format12Hour="yyyy年MM月dd日 H:mma EEEE"
            android:drawableEnd="@mipmap/ic_launcher"></TextClock>
        <Chronometer
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30dp"></Chronometer>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

下面是来控制布局界面的java代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main9);

        ToggleButton toggle = findViewById(R.id.toggle);
        Switch switcher = findViewById(R.id.switcher);

        LinearLayout layout = findViewById(R.id.test);
        CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    layout.setOrientation(LinearLayout.VERTICAL);
                    toggle.setChecked(true);
                    switcher.setChecked(true);
                }else {
                    layout.setOrientation(LinearLayout.HORIZONTAL);
                    toggle.setChecked(false);
                    switcher.setChecked(false);
                }
            }
        };
        toggle.setOnCheckedChangeListener(listener);
        switcher.setOnCheckedChangeListener(listener);
    }

xml文件后面还列举了三个和时间相关的内容,第一个是安卓系统自带的一款显示当前时间的时钟。

 <AnalogClock
            android:layout_width="100dp"
            android:layout_height="100dp"></AnalogClock>

后面是自定义格式定义的一个数字时间展示

        <TextClock
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:textColor="#f0f"
            android:format12Hour="yyyy年MM月dd日 H:mma EEEE"
            android:drawableEnd="@mipmap/ic_launcher"></TextClock>

接下来是安卓系统附带的一款倒计时功能,需要结合java代码实现。

        <Chronometer
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30dp"></Chronometer>

🔍三,动画

逐帧动画:需要很多的图片资源的支持。

补间动画:

  1. 透明度渐变动画
  2. 旋转动画
  3. 缩放动画
  4. 平移动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--透明度-->
    <alpha android:fromAlpha="0"
        android:toAlpha="1"
        android:duration="2000"></alpha>
    <!--缩放动画-->
    <scale android:fromXScale="0"
        android:fromYScale="0"
        android:toXScale="90"
        android:toYScale="90"
        android:pivotX="45"
        android:pivotY="45"></scale>
    <!--旋转动画-->
    <rotate android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="4000"></rotate>
    <!--平移动画-->
    <translate android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="50"
        android:toYDelta="50"></translate>
</set>

🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱

📌内容总结:

🕥本文介绍了Context 和动画的一部分内容给,中间记录了一些源码,还有🐛没有写到本文内,后续内容可以继续追踪博主的后续文章,或许会介绍相关的内容,如果没介绍,请用力踢一脚,好让摸鱼的博主积极主动的去认识错误并及时改正,在发光发热的道路上越走越远……


表情网站:🎁 Emoji cheat sheet for GitHub, Basecamp, Slack & more (webfx.com)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NoSuchManException

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值