听说点赞关注的人,身体健康,万事如意,工作顺利,爱情甜蜜,一夜暴富,升职加薪……最终迎娶白富美!!!
‼️微信公众号:炜煜工作室
🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱
📡 :安卓开发-基础知识补习13💯
📰内容简介:
本文介绍了Context 和动画的一部分内容给,中间记录了一些源码,使用java代码进行开发,如果有问题的地方请不吝指教,如果对文内内容有不理解的地方,也希望能积极主动的联系博主进行深刻的探讨,以便于让博主更深刻的记住这篇博文的内容,好让博主在发光发热的道路上越走越远。[手动狗头]🐶努力,奋斗!
📎 标签:安卓;java;HttpURLConnection;
🔍一、Context 上下文
-
Context:上下文、语境、环境。安卓系统中,可以理解为当前对象在应用程序中所处的工作环境。其内部定义很多访问应用程序环境中全局信息的接口,通过它可以访问到应用程序的资源有关的类。如:Resoutces,AssetManager,Package及权限。
-
Context 提供了关于应用环境全局信息的接口。它是一个抽象类,它的执行被 Android 系统所提供。它允许获取以应用为特征的资源和类型,是一个统领一些资源(应用程序环境变量等)的上下文。
-
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mpelJC9O-1667989062730)(C:\Users\29933\AppData\Roaming\Typora\typora-user-images\image-20221108100303661.png)]
-
Context:是一个抽象类,定义一系列与系统交互的接口。
-
ContextWrapper:继承自Context类,是Context类的包装类(装饰器模式),内部维护一个Context类型的成员变量,mBase指向一个ContextImpl对象,ContextWrapper里面的方法调用时通过mBase来调用ContextImpl里面的方法,这里用到了代理模式。
-
ContextImpl:继承自Context类,实现了Context类中抽象方法,时Context类的具体实现类。它我iActivity及其应用组件提供行下文环境,应用中使用到的Context的方法就是其实现的。
-
ContextThemeWrapper:继承自ContextWrapper类,在ContextWrappwe的基础上增加与主题Theme相关的逻辑,即可以指定Theme的Context包装类,用于在View构造时为其提供Theme属性集。
ContextImpl实现类中设计的主要核心类是:ActivityThread、LoadedApk、PackageManager和ResourcesManager,这几个类都是单例的,一个应用程序进程中是共用同一个对象的。 ContextImpl是一种轻量级类,而LoadeApk是一个重量级类,ContextImpl中的大多数及逆行包操作的重量级函数世界上都是转向了LoadedApk对象相应的方法。 Activity继承自ContextThremeWrapper,Application,Sevice继承自ContextWrapper,他们直接或间接继承自ContextWrapper类,因此也拥有了一个Context类型的成员变量mBase指向一个ContextImpl对象,ContextImpl是Context类的具体实现类,所以也都拥有了Context提供的所有功能。 代理模式:属于结构模式,也指为其他对象提供一种个代理以控制对这个对象的访问,代理模式又分为静态代理和动态代理,动态代理包含CGLib代理。 装饰器模式:又叫做包装模式,也是结构性模式,是指在不改变现有对象结构的情况下下,动态的给该对象增加一些职责(即增加其额外的功能)的模式。
-
Context的作用域
Context作用域 Appliation Activity Service Show a Dialog No YES NO Start an Activity 不推荐 YES 不推荐 Layout Inflation 不推荐 YES 不推荐 Start a Service YES YES YES Send a Broadcast YES YSE YES Register Broadcast Receicer YES YES YES Load Resource Values YES YES YES 注意:需要加载布局显示界面的,尽可能使用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>
🔍三,动画
逐帧动画:需要很多的图片资源的支持。
补间动画:
- 透明度渐变动画
- 旋转动画
- 缩放动画
- 平移动画
<?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)