???????????????????????????????????????????????????? 控件
TextView?? 文本框
wrap_content:是layout_width和layout_height的屬性值之一,表示和自身內(nèi)容一樣的長(zhǎng)度。match_parent:是layout_width和layout_height的屬性值之一,表示和父組件一樣的長(zhǎng)度。?
<?xml version="1.0" encoding="utf-8"?><LinearLayout? xmlns:android="http://schemas.android.com/apk/res/android"???????????????? android:layout_height="match_parent"???????????????? android:orientation="vertical"??????????????? android:layout_width="match_parent"???????????? >??? <TextView??????? android:id="@+id/one"??????? android:layout_width="200dp"??????? android:layout_height="200dp"??????? android:text="@string/app_text"??????? android:textSize="30sp"??????? android:textStyle="bold"??????? android:gravity="center"??????? android:textColor="@color/red"??? />?
android:shadowColor="@color/colorPrimaryDark"android:shadowRadius="100.0"android:shadowDy="20.0"android:shadowDx="20.0"android:singleLine="true"
android:focusable="true"android:focusableInTouchMode="true"android:ellipsize="marquee"android:marqueeRepeatLimit="marquee_forever"android:clickable="true"??????????????????????????????
????????????????????????????????? ??? 第2方法
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">?? <item android:color="@color/colorPrimaryDark" android:state_pressed="true"/>??? <item android:color="@color/red"/></selector><?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">??? <item? android:drawable="@drawable/ic_extension_black_24dp"? android:state_pressed="true"/>??? <item android:drawable="@drawable/ic_event_black_24dp"/></selector>?android:background="@drawable/btn_selector"???? <!--背景圖片-->android:backgroundTint="@color/btn_color"????? <!--背景顏色-->????????
????????????????????????????????????????? Button事件處理
Button button1 =
findViewById(R.id.btn1);
[if !supportLists]1.?????[endif]點(diǎn)擊事件
//點(diǎn)擊事件button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(Viewview) {
??????? System.
out.println("點(diǎn)擊事件啟動(dòng)");
??? }
});
[if !supportLists]2.?????[endif]長(zhǎng)按事件
//長(zhǎng)按事件button1.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
??????? System.
out.println("長(zhǎng)按事件啟動(dòng)");
return false;
??? }
});
[if !supportLists]3.?????[endif]觸摸事件
??? //觸摸事件
??? button1.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
??????????? System.
out.println("觸摸事件啟動(dòng)");
return false;
??????? }
??? });
}
[if !supportLists]4.?????[endif]第二種點(diǎn)擊事件方法
android:onClick="xmlonClick"
?? public voidxmlonClick(View view) {
??? System.
out.println("在xml文件觸發(fā)點(diǎn)擊事件");
}
?????????????????????????????? EditText?? 輸入框
<EditText??? android:layout_width="179dp"??? android:layout_height="101dp"??? android:drawableLeft="@drawable/ic_perm_identity_black_24dp"??? android:drawablePadding="15dp"??? android:hint="請(qǐng)輸入用戶名"??? android:inputType="text"??? android:paddingLeft="10dp"??? android:textColorHint="@color/colorPrimaryDark"??? android:background="@color/white"?? ?></EditText>
通過(guò)點(diǎn)擊事件獲取文本信息btn= findViewById(R.id.btn);ed=findViewById(R.id.ed);btn.setOnClickListener(new View.OnClickListener(){??? @Override??? public void onClick(Viewview) {??????? String s = ed.getText().toString();??????? System.out.println(s);??? }});??????????????????????????????? ? ? ? ? ? ImageView? 圖像視圖
<ImageView??? android:layout_width="200dp"??? android:layout_height="200dp"??? android:src="@drawable/a"??? android:maxHeight="200dp"??? android:maxWidth="200dp"??? android:adjustViewBounds="true"></ImageView>?????????????????? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ProgressBar?? 進(jìn)度條
<ProgressBar??? android:id="@+id/pg"??? android:layout_width="wrap_content"??? android:layout_height="wrap_content"></ProgressBar><Button??? android:id="@+id/btnpg"??? android:layout_width="200dp"??? android:layout_height="wrap_content"?? android:text="點(diǎn)擊隱藏進(jìn)度"??? android:onClick="onClickHidden"></Button>public void onClickHidden(View view){??? if (pg.getVisibility() == View.GONE){??????? pg.setVisibility(View.VISIBLE);??? }else {??????? pg.setVisibility(View.GONE);??? }}??<ProgressBar??? android:id="@+id/load"??? style="?android:attr/progressBarStyleHorizontal"??? android:layout_width="500dp"??? android:layout_height="wrap_content"??? android:max="500"??? >??????????? 第二種</ProgressBar>?<Button???? android:id="@+id/btnload"???? android:layout_width="200dp"???? android:layout_height="wrap_content"???? android:text="模擬下載"???? android:onClick="onload">?</Button>?<ProgressBar???? android:id="@+id/loading"???? style="?android:attr/progressBarStyleHorizontal"???? android:layout_width="500dp"???? android:layout_height="wrap_content"???? android:max="100"???? android:indeterminate="true"???? />public void onload(View view) {??? load=findViewById(R.id.load);??? System.out.println("模擬下載");??? int progress= load.getProgress();??? progress +=100;??? if (progress> 500)??????? System.out.println("下載完成");??? load.setProgress(progress);}???????????????????????? ??????? ??? Notification?? 狀態(tài)通知欄
manger= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if ( Build.VERSION.SDK_INT? >=? Build.VERSION_CODES.O ) {
??? NotificationChannel channel =
new NotificationChannel("channelId", "測(cè)試通知", NotificationManager.IMPORTANCE_HIGH);
manger.createNotificationChannel(channel);
}
Intent intent =
new Intent(this, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0,intent, 0);
notification=new NotificationCompat. Builder(this,"channelId")
??????? .setContentTitle(
"標(biāo)題")?? //設(shè)置標(biāo)題
??????? .setContentText("正文內(nèi)容")? //設(shè)置文本內(nèi)容
??????? .setSmallIcon(R.drawable.ic_event_black_24dp)? //設(shè)置小圖標(biāo)
??????? .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.a))
??????? .setColor(Color.parseColor(
"#0000FF"))
??????? .setContentIntent(pendingIntent)
??????? .setAutoCancel(
true)
??????? .build() ;
?
public void sendification(View view) {
manger.notify(1,notification);
}
public void unsendNotification(View view) {
manger.cancel(1);
}
@Override
protected void onCreate(@NullableBundle savedInstanceState) {
super.onCreate(savedInstanceState);
??? Log.e(
"leo","getMeassge:進(jìn)入NotificationActivity");
}
?? ???????????? ?Toolbar??標(biāo)題欄
<androidx.appcompat.widget.Toolbar/>
包導(dǎo)錯(cuò)了 ?要導(dǎo)入 androidx的包??????????????????????????????????????????????????????????????????????????????????????????????????? 第一種方式<androidx.appcompat.widget.Toolbar??? android:id="@+id/tb"??? android:layout_width="match_parent"??? android:layout_height="?attr/actionBarSize"??? android:background="@color/blue"??? app:title="標(biāo)題"??? app:navigationIcon="@drawable/ic_trending_flat_black_24dp"??? app:titleMarginStart="100dp"??? app:subtitle="子標(biāo)題"??? app:subtitleTextColor="@color/colorPrimary"??? app:logo="@drawable/ic_perm_identity_black_24dp"??? />Toolbar toolbar=findViewById(R.id.tb);???????? //設(shè)置導(dǎo)航單擊偵聽(tīng)器 ?toolbar.setNavigationOnClickListener(new View.OnClickListener() {???? @Override???? public void onClick(Viewv) {???????? Log.e("leo", "toolbar被點(diǎn)擊了" );???? }?});???????????????????????????????????? 第二種方式<androidx.appcompat.widget.Toolbar??? android:layout_marginTop="20dp"??? android:id="@+id/tb2"??? android:layout_width="match_parent"??? android:layout_height="?attr/actionBarSize"??? android:background="@color/colorPrimaryDark"??? />toolbar1.setNavigationOnClickListener(new View.OnClickListener() {??? @Override??? public void onClick(Viewv) {??????? Log.e("leo", "toolbar22222被點(diǎn)擊了" );??? }});???????????? 標(biāo)題居中<androidx.appcompat.widget.Toolbar??? android:layout_marginTop="20dp"??? android:id="@+id/tb3"??? android:layout_width="match_parent"??? android:layout_height="?attr/actionBarSize"??? android:background="@color/red">??? <TextView??????? android:layout_height="wrap_content"??????? android:layout_width="wrap_content"??????? android:text="標(biāo)題"??????? android:layout_gravity="center"??????? /></androidx.appcompat.widget.Toolbar>?????? ?????????????? AlertDialog?? 對(duì)話欄
?<Button??? android:layout_marginTop="20dp"??? android:layout_width="100dp"??? android:layout_height="100dp"??? android:text="顯示對(duì)話框"??? android:onClick="leoclick"??? />???public void leoclick(View view) {??? //獲得對(duì)話框??? AlertDialog.Builder builder = new AlertDialog.Builder(this);??? //獲得布局??? View inflate =getLayoutInflater().inflate(R.layout.dialog_view, null);??? builder.setIcon(R.drawable.ic_event_black_24dp)??????????? .setTitle("標(biāo)題")??????????? .setMessage("發(fā)送信息")??????????? .setView(inflate)??????????? .setPositiveButton("確定", new DialogInterface.OnClickListener() {??????????????? @Override??????????????? public void onClick(DialogInterface dialog, int which) {??????????????????? System.out.println("點(diǎn)擊了確定按鈕");??????????????? }??????????? })??????????? .setNegativeButton("取消", new DialogInterface.OnClickListener() {??????????????? @Override??????????????? public void onClick(DialogInterface dialog, int which) {??????????????????? System.out.println("點(diǎn)擊了取消按鈕");??????????????? }??????????? })??????????? .setNeutralButton("中間", new DialogInterface.OnClickListener() {??????????????? @Override??????????????? public void onClick(DialogInterface dialog, int which) {??????????????????? System.out.println("點(diǎn)擊了中間按鈕");??????????????? }??????????? })??????????? .show()??????????? .create();}??<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"??? android:orientation="vertical" android:layout_width="match_parent"??? android:layout_height="match_parent"??? android:background="@color/colorPrimary">??? <ImageView??????? android:layout_width="200dp"??????? android:layout_height="100dp"??????? android:src="@drawable/b"/>??? <TextView??????? android:layout_width="wrap_content"??????? android:layout_height="wrap_content"??????? android:text="圖片"/></LinearLayout>??????????????????????? PopupWindow? 懸浮框
????????????? 兩種構(gòu)造方法 ?//獲得布局??? View inflate = getLayoutInflater().inflate(R.layout.propup_view, null);??? //獲得懸浮框????????ViewGroup.LayoutParams.WRAP_CONTENT相當(dāng)于wrap_content??? final PopupWindow popupWindow = new PopupWindow(inflate,ViewGroup.LayoutParams.WRAP_CONTENT,?????????????????????????????????????????????ViewGroup.LayoutParams.WRAP_CONTENT,true);??? //設(shè)置popupWindow背景??? popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.b));??? //獲取propup_view下的屬性??? Button btn1= inflate.findViewById(R.id.btn1);??? Button btn2=inflate.findViewById(R.id.btn2);??? btn1.setOnClickListener(new View.OnClickListener() {??????? public void onClick(Viewv) {??????????? System.out.println("你是來(lái)自北京嗎");??????????? popupWindow.dismiss();??????? }??? });??? btn2.setOnClickListener(new View.OnClickListener() {??????? @Override??????? public void onClick(Viewv) {??????????? System.out.println("你是來(lái)自上海嗎");??????????? popupWindow.dismiss();??????? }??? });??? //顯示popupWindow??? popupWindow.showAsDropDown(view,200,view.getHeight());}??<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"??? android:orientation="vertical" android:layout_width="match_parent"??? android:layout_height="match_parent"??? android:background="@drawable/ic_launcher_background">??? <Button??????? android:id="@+id/btn1"??????? android:layout_marginTop="20dp"??????? android:layout_width="100dp"??????? android:layout_height="100dp"??????? android:text="北京"??????? android:background="@color/red"??????? />??? <Button??????? android:id="@+id/btn2"??????? android:layout_marginTop="20dp"??????? android:layout_width="100dp"??????? android:layout_height="100dp"??????? android:text="上海"??????? android:background="@color/blue"??????? /></LinearLayout>??<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"??? android:layout_height="match_parent"??? xmlns:app="http://schemas.android.com/apk/res-auto"??? android:orientation="vertical"??? android:layout_width="match_parent">??? <Button??????? android:id="@+id/pw"??????? android:layout_marginTop="20dp"??????? android:layout_width="100dp"??????? android:layout_height="100dp"??????? android:text="彈出PopupWindow"??????? android:onClick="popupWindowClick"??????? />???????????? 布局??? LinearLayout? 線性布局
??? RelativeLayout 相對(duì)布局
TableLayout 表格布局<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"??? android:orientation="vertical" android:layout_width="match_parent"??? android:layout_height="match_parent">??? <Button??????? android:layout_height="wrap_content"??????? android:layout_width="wrap_content"??????? android:text="按鍵1"??????? />??? <TableRow>??? <Button??????? android:layout_height="wrap_content"??????? android:layout_width="wrap_content"??????? android:text="按鍵1"??????? />??? <Button??????? android:layout_height="wrap_content"??????? android:layout_width="wrap_content"??????? android:text="按鍵2"??????? />??? <Button??????? android:layout_height="wrap_content"??????? android:layout_width="wrap_content"??????? android:text="按鍵3"??????? />?? </TableRow>??? <TableRow>??????? <Button??????????? android:layout_height="wrap_content"??????????? android:layout_width="wrap_content"??????????? android:text="按鍵1"??????????? />??????? <Button??????????? android:layout_height="wrap_content"??????????? android:layout_width="wrap_content"??????????? android:text="按鍵2"??????????? />??????? <Button??????????? android:layout_height="wrap_content"??????????? android:layout_width="wrap_content"??????????? android:text="按鍵3"??????????? />??????? <Button??????????? android:layout_height="wrap_content"??????????? android:layout_width="wrap_content"??????????? android:text="按鍵4"??????????? />??????? <Button??????????? android:layout_height="wrap_content"??????????? android:layout_width="wrap_content"??????????? android:text="按鍵5"??????????? />??? </TableRow></TableLayout>
<?xml version="1.0" encoding="utf-8"?><GridLayout xmlns:android="http://schemas.android.com/apk/res/android"??? android:layout_width="match_parent" android:layout_height="match_parent"??? android:orientation="horizontal"??? android:columnCount="2"??? >??? <Button??????? android:layout_width="wrap_content"??????? android:layout_height="wrap_content"??????? android:text="圖片"/>??? <Button??????? android:layout_width="wrap_content"??????? android:layout_height="wrap_content"??????? android:text="圖片"/>??? <Button??? android:layout_width="wrap_content"??? android:layout_height="wrap_content"??? android:text="圖片"/>??? <Button??? android:layout_width="wrap_content"??? android:layout_height="wrap_content"??? android:text="圖片"/>??? <Button??? android:layout_width="wrap_content"??? android:layout_height="wrap_content"??? android:text="圖片"/></GridLayout>
行 列 ?第一行第0?????????????????????????????????????????????????????????????????????????????????? ConstraintLayout 約束布局
?基礎(chǔ)開(kāi)發(fā)------ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? xmlns:tools="http://schemas.android.com/tools"
??? android:layout_width="match_parent"
??? android:layout_height="match_parent"
??? tools:context=".MainActivity">
??? <
ListView
??????? android:id="@+id/lv"
??????? android:layout_width="match_parent"
??????? android:layout_height="match_parent">
??? </
ListView>
</
LinearLayout>
<?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"
??? android:orientation="vertical"
??? android:layout_width="match_parent"
??? android:layout_height="match_parent">
??? <
TextView
??????? android:id="@+id/tv"
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:textSize="20dp"
??????? android:background="@color/colorPrimary"
??????? />
</
LinearLayout>
??????? package bibipor.ma;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Bean> data=new ArrayList();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
??????? setContentView(R.layout.
activity_main);
for (int i=0;i<100;i++){
??????????? Bean bean=
new Bean();
??????????? bean.setName(
"滑動(dòng)欄"+ "--:---" + i);
data.add(bean);
??????? }
??????? ListView listView= findViewById(R.id.
lv);
??????? listView.setAdapter(
new MyAdapter(data,this));
??????? listView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
??????????????? System.
out.println(position+"被點(diǎn)擊了");
????? ??????}
??????? });
??? }
}
package bibipor.ma;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extendsBaseAdapter {
private List<Bean>data;
private Contextcontext;
public MyAdapter(Listdata, Context context) {
this.data = data;
this.context = context;
??? }
//獲取總共數(shù)據(jù)
??? public int getCount() {
return data.size();
??? }
//獲取item
??? public Object getItem(int position){
return null;
??? }
//獲取item-id
??? public long getItemId(intposition) {
return position;
??? }
//返回的view
??? public View getView(intposition, View convertView, ViewGroupparent) {
??????? ViewHolder viewHolder;
if (convertView? ==? null) {
??????????? viewHolder=
new ViewHolder();
??????????? convertView = LayoutInflater.from(
context).inflate(R.layout.list_view, parent, false);
??????????? viewHolder.
textView = convertView.findViewById(R.id.tv);
???????????convertView.setTag(viewHolder);
?? ?????}
else{
??????????? viewHolder= (ViewHolder)convertView.getTag();
??????? }
//? TextViewtextView = convertView.findViewById(R.id.tv);
??????? viewHolder.textView.setText(data.get(position).getName());
??????? Log.e(
"leo", "getView:
"+position);
return convertView;
??? }
private final class ViewHolder{
??????? TextView
textView;
??? }
}
???????????????????????????????????????? RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? xmlns:tools="http://schemas.android.com/tools"
??? android:layout_width="match_parent"
??? android:layout_height="match_parent"
??? tools:context=".MainActivity"
??? android:orientation="horizontal">
??? <androidx.recyclerview.widget.RecyclerView
??????? android:id="@+id/rv"
??????? android:layout_width="match_parent"
??????? android:layout_height="match_parent" />
</LinearLayout>
?
?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? android:orientation="vertical"
??? android:layout_width="match_parent"
??? android:layout_height="match_parent">
??? <TextView
??????? android:id="@+id/tv"
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:textSize="20dp"
??????? android:background="@color/colorPrimary"
??????? />
</LinearLayout>
?
?
for (int i=0;i<100;i++){
??? Bean bean=new Bean();
??? bean.setName("滑動(dòng)欄"+ "--:---" + i);
??? data.add(bean);
}
RecyclerView recyclerView=findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
MyAdapterRecyclerView??
myAdapterRecyclerView=new MyAdapterRecyclerView(data,this);
recyclerView.setAdapter(myAdapterRecyclerView);
myAdapterRecyclerView.setOnRecyclerItemClickListener(new MyAdapterRecyclerView.OnRecyclerItemClickListener() {
??? @Override
??? public void OnRecyclerItemClick(int position) {
??????? System.out.println("點(diǎn)擊了"+position);
??? }
});
?
?
?
package bibipor.ma;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
class MyAdapterRecyclerView extends RecyclerView.Adapter<MyAdapterRecyclerView.MyViewHoider>
{
??? private List<Bean>
data;
??? private Context
context;
??? public MyAdapterRecyclerView(List<Bean>
data, Context context) {
??????? this.data = data;
??????? this.context = context;
??? }
??? @NonNull
??? //創(chuàng)建MyViewHoider
??? public MyViewHoider onCreateViewHolder(@NonNull ViewGroup
parent, int viewType) {
??????? View view=View.inflate(context,R.layout.recycler_view,null);
??????? return new MyViewHoider(view);
??? }
??? //綁定數(shù)據(jù)
??? public void onBindViewHolder(@NonNull MyViewHoider
holder, int position) {
??????? holder.tv.setText(data.get(position).getName());
??? }
??? @Override
??? public int getItemCount()
{
??????? return data ==? null
? 0 :data.size();
??? }
??? public class MyViewHoider extends
RecyclerView.ViewHolder {
??????? private TextView
tv;
??????? public MyViewHoider(@NonNull View itemView) {
?????????? super(itemView);
??????? tv=itemView.findViewById(R.id.tv);
??????? itemView.setOnClickListener(new View.OnClickListener() {
??????????? @Override
??????????? public void onClick(View
v) {
??????????????? onRecyclerItemClickListener.OnRecyclerItemClick(getAdapterPosition());
??????????? }
??????? });
??????? }
??? }
??? private OnRecyclerItemClickListener
onRecyclerItemClickListener;
??? public void? setOnRecyclerItemClickListener(OnRecyclerItemClickListener
listener){
??????? onRecyclerItemClickListener = listener;
??? }
??? public interface? OnRecyclerItemClickListener{
??????? void? OnRecyclerItemClick(int position);
??? }
}
?
動(dòng)畫(huà)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? android:id="@+id/rl"
??? android:layout_width="match_parent"
??? android:layout_height="match_parent"
??? android:background="@drawable/frame"/>
?
?
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
??? <item android:drawable="@drawable/a" android:duration="120"></item>
??? <item android:drawable="@drawable/b" android:duration="120"></item>
??? <item android:drawable="@drawable/c" android:duration="120"></item>
??? <item android:drawable="@drawable/d" android:duration="120"></item>
??? <item android:drawable="@drawable/e" android:duration="120"></item>
??? <item android:drawable="@drawable/f" android:duration="120"></item>
??? <item android:drawable="@drawable/g" android:duration="120"></item>
??? <item android:drawable="@drawable/h" android:duration="120"></item>
</animation-list>
?
?
?
package donghua.ma;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
public class MainActivity extends
AppCompatActivity {
??? private? boolean flag=true;
??? protected void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.activity_main);
??????? RelativeLayout relativeLayout=
findViewById(R.id.rl);
??????? //獲取動(dòng)畫(huà)資源
??????? final AnimationDrawable anima= (AnimationDrawable)
relativeLayout.getBackground();
???????
relativeLayout.setOnClickListener(new View.OnClickListener()
{
??????????? @Override
??????????? public void onClick(View
v) {
??????????????? if (flag){
??????????????????? //開(kāi)啟動(dòng)畫(huà)
??????????????????? anima.start();
??????????????????? flag= ! flag;
??????????????? }else {
??????????????????? //關(guān)閉資源
??????????????????? anima.stop();
??????????????????? flag=! flag;
??????????????? }
??????????? }
??????? });
??? }
}
?
?
?? ??
<ImageView
??? android:id="@+id/iv"
??? android:layout_width="wrap_content"
??? android:layout_height="wrap_content"
??? android:src="@drawable/f"
??? android:adjustViewBounds="true">
</ImageView>
?
???????????????????????????? alpha透明度
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
??? <!--fromAlpha?
toAlpha? 透明度的變化?
duration? 變化時(shí)間間隔-->
??? <alpha
??????? android:duration="2000"
??????? android:toAlpha="0"
?????? android:fromAlpha="1"
?????? />
</set>
?
?
imageView.setOnClickListener(new View.OnClickListener() {
??? @Override
??? public void onClick(View
v) {
??????? //通過(guò)加載xml動(dòng)畫(huà)設(shè)置文件創(chuàng)建一個(gè) Animation對(duì)象
??????? Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
??????? //表示imageView去啟動(dòng)animation 這個(gè)動(dòng)畫(huà)
??????? imageView.startAnimation(animation);
??? }
});
?
?
?
??????????????????? ??? <!-- scale縮放-->
?
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
??? <!--縮放-->
??? <scale
??????? android:interpolator=
"@android:anim/decelerate_interpolator"
??????? android:duration="2000"
??????? android:fromXScale="1.0"
??????? android:fromYScale="1.0"
??????? android:pivotX="50%"
??????? android:pivotY="50%"
??????? android:repeatCount="0"
??????? android:repeatMode="restart"
??????? android:fillAfter="true"
??????? android:startOffset="0"
??????? android:toXScale="0.5"
??????? android:toYScale="0.5" />
</set>
?
?
???????????????????????? ???? <!-- rotate旋轉(zhuǎn)-->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
??? <!--旋轉(zhuǎn)-->
??? <rotate
??????? android:interpolator="@android:anim/accelerate_interpolator"
??????? android:fromDegrees="0"
??????? android:toDegrees="360"
??????? android:pivotX="50%"
??????? android:pivotY="50%"
??????? android:duration="2000"
??????? android:repeatCount="5"
??????? android:repeatMode="restart"/>
</set>
????????? <!-- translate平移-->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
??? <!--平移-->
??? <translate
??????? android:fromXDelta="0"
??????? android:fromYDelta="0"
??????? android:toXDelta="400"
??????? android:toYDelta="400"
??????? android:duration="2000"
??????? />
</set>
?
?
?
?
?
???????????????????????????????????????????????????????????????????????????????????? 屬性動(dòng)畫(huà)
?????????????????????????????????????????????????????????????????????????????????? 改變value值
package bibi.ma;
import androidx.appcompat.app.AppCompatActivity;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends
AppCompatActivity {
??? @Override
??? protected void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.activity_main);
??????? ValueAnimator valueAnimator =
ValueAnimator.ofFloat(0f, 1f);
??????? valueAnimator.setDuration(20000);
??????? valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
??????????? private int i;
??????????? @Override
??????????? public void onAnimationUpdate(ValueAnimator
animation) {
??????????????? float?? value= (float)
animation.getAnimatedValue();
??????????????? TextView tv =
findViewById(R.id.tv);
?????????????? Log.e("leo", "onAnimationUpdate:
"+value );
??????????????? tv.setText(value+"");
??????????? }
??????? });
??????????????? valueAnimator.start();
??? }
}
TextView textView=
findViewById(R.id.tv2);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,
??????????????????????????????? "alpha",0f,1f
);
objectAnimator.setDuration(20000);
objectAnimator.start();
?
?
?
?
@Override
public void onAnimationCancel(Animator animation) {
??? super.onAnimationCancel(animation);
??? Log.e("leo", "動(dòng)畫(huà)取消了: " +new
Date());
}
@Override
public void onAnimationRepeat(Animator animation) {
??? super.onAnimationRepeat(animation);
??? Log.e("leo", "動(dòng)畫(huà)關(guān)閉了: " +new
Date());
}
@Override
public void onAnimationStart(Animator animation) {
??? super.onAnimationStart(animation);
??? Log.e("leo", "動(dòng)畫(huà)開(kāi)始了: " +new
Date());
}
@Override
public void onAnimationPause(Animator animation) {
??? super.onAnimationPause(animation);
??? Log.e("leo", "動(dòng)畫(huà)暫停了: " +new
Date());
}
@Override
public void onAnimationResume(Animator animation) {
??? super.onAnimationResume(animation);
??? Log.e("leo", "動(dòng)畫(huà)繼續(xù)了: " +new
Date());
}
?
@Override
public void onAnimationEnd(Animator animation) {
??? super.onAnimationResume(animation);
??? Log.e("leo", "動(dòng)畫(huà)結(jié)束了: " +new
Date());
}
??????