创建drawable

导入照片
创建布局标红的是重要部分
动画的开启关闭
属性动画
package com.example.day6_;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Button brStart;
private Button brStop;
private ImageView lv;
private AnimationDrawable manimationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
manimationDrawable=(AnimationDrawable) lv.getBackground();//得到帧动画
}
public void startFrame(View view) {
if (!manimationDrawable.isRunning()){
manimationDrawable.start();
}
}
public void stopFrame(View view) {
if (manimationDrawable.isRunning()){
manimationDrawable.stop();
}
}
private void initView() {
brStart = (Button) findViewById(R.id.br_start);
brStop = (Button) findViewById(R.id.br_stop);
lv = (ImageView) findViewById(R.id.lv);
}
public void alpha(View view) {
Animation animation = AnimationUtils.loadAnimation(this,R.anim.alpha);//加载动画
lv.startAnimation(animation);
}
//属性动画
public void shuxing(View view) {
//最后一位写成0就能实现消失
ObjectAnimator alpha= ObjectAnimator.ofFloat(lv,"alpha",0,1,0.5f,1,1,0.5f);
// alpha.setDuration(3000);
// alpha.start();
//旋转
ObjectAnimator rotation= ObjectAnimator.ofFloat(lv,"Rotation",0,180,20,-180);
rotation.setDuration(3000);
rotation.start();
//缩放
ObjectAnimator scaleX= ObjectAnimator.ofFloat(lv,"scaleX",1,3,1,-1);
//平移
ObjectAnimator translationX= ObjectAnimator.ofFloat(lv,"translationX",0,200,0,-300);
//组合动画
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(3000);
animatorSet.start();
// animatorSet.playTogether(alpha,rotation,scaleX,translationX);//一起播放
animatorSet.play(alpha).before(rotation).before(scaleX).after(translationX);
}
}
侧拉窗口:
侧拉布局:

布局代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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=".DrawerLayoutActivity"
tools:ignore="MissingDefaultResource"
android:id="@+id/dr"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/but"
android:text="打开抽屉"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nag"
//连接布局
app:headerLayout="@layout/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
//连接布局
app:menu="@menu/menu"
android:layout_gravity="start">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
创建连接layout布局和主布局连接
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/header_iv"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:padding="5dp"
android:src="@mipmap/ic_launcher_round">
</ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/header_tv"
android:gravity="center"
android:padding="10dp"
android:text="tc">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/header_desc"
android:gravity="center"
android:padding="10dp"
android:text="这里是侧拉菜单的描述,可以写一封资料">
</TextView>
</LinearLayout>
创建menu布局

menu布局代码实现:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/aa"
android:icon="@drawable/aa" android:title="首页"/>
<item android:id="@+id/bb"
android:icon="@drawable/bb" android:title="首页"/>
<item android:id="@+id/cc"
android:icon="@drawable/cc" android:title="首页"/>
</menu>
Activity页面
package com.example.day9_touchevent1;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import com.bumptech.glide.Glide;
import com.google.android.material.navigation.NavigationView;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;
public class DrawerLayoutActivity extends AppCompatActivity {
private DrawerLayout dr;
private Button but;
private NavigationView nag;
private ImageView viewById;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_layout);
initView();
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dr.openDrawer(Gravity.LEFT);
}
});
//可以添加多个头布局,当前0代表获取index为0的头布局
//可以获取layout里面的id
View headerView = nag.getHeaderView(0);
TextView user =headerView.findViewById(R.id.header_tv);
user.setText("周坤");
//menu联动
nag.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
if (item.getItemId()==R.id.aa){
Toast.makeText(DrawerLayoutActivity.this, "点击了home", Toast.LENGTH_SHORT).show();
dr.close();
}else if (item.getItemId()==R.id.bb){
Toast.makeText(DrawerLayoutActivity.this, "点击了chat", Toast.LENGTH_SHORT).show();
dr.close();
}else {
Toast.makeText(DrawerLayoutActivity.this, "点击了mine", Toast.LENGTH_SHORT).show();
dr.close();
}
return false;
}
});
//通过headerView拿到图片id
viewById = headerView.findViewById(R.id.header_iv);
viewById.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PictureSelector.create(DrawerLayoutActivity.this,101).selectPicture();
}
});
}
//更换头像
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==101&&resultCode==RESULT_OK){
PictureBean pictureSelector= (PictureBean) data.getExtras().get(PictureSelector.PICTURE_RESULT);
Glide.with(this).load(pictureSelector.getPath()).into(viewById);
}
}
private void initView() {
dr = (DrawerLayout) findViewById(R.id.dr);
but = (Button) findViewById(R.id.but);
nag = (NavigationView) findViewById(R.id.nag);
}
}
效果展示:

1327




被折叠的 条评论
为什么被折叠?



