day04_Toolbar+SlidingMenu+day05_SurfaceView

1.Toolbar和DrawerLayout实现
Toolbar:标题栏
DrawerLayout:可以实现侧滑
2.SlideMenu实现 第三方 需要导入moudle(slidemenu)
ToolBar常用的方法
Toolbar是在 Android 5.0 开始推出的一个 Material Design 风格的导航控件,以此来取代之前的Actionbar 。我们需要在工程中引入appcompat-v7的兼容包以便向下兼容, 使用android.support.v7.widget.Toolbar进行开发。在设计 Toolbar 的时候,Google也留给了开发者很多可定制修改的余地,这些可定制修改的属性在API文档中都有详细介绍,如:

1.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);去掉标题栏;
2.Toolbar.setLogo(),设置logo图片;
3.Toolbar.setTitle().设置标题;
4.Toolbar.setSubTitle()设置子标题;
5.Toolbar.setTitleTextColor(int color);设置标题文字颜色;
6.Toolbar.setSubtitleTextColor();设置子标题文字颜色;
7.setTitleMargin(int start, int top, int end, int bottom);设置标题margin值; 8.onCreateOptionsMenu,getMenuInflater().inflate(R.menu.menu,menu)
设置菜单在给Toolbar设置为actionbar时使用;
9.Toolbar.setOnMenuItemClickListener();Toolbar绑定menu监听;
10.Toolbar.inflateMenu(R.menu.menu)在Toolbar没有替换actionbar时使用;
11.setSupportActionBar(mToolbar);设置toolbar替换actionbar;
12.getLayoutInflater().inflate(R.layout.view_tv,bar);Toolbar添加自定义view
DrawerLayout常用的方法
DrawerLayout.isDrawerOpen(Gravity.LEFT)是否开启;
DrawerLayout.openDrawer(Gravity.LEFT);开启抽屉
DrawerLayout.closeDrawer(Gravity.RIGHT);关闭抽屉
在这里插入图片描述
在这里插入图片描述

public class MainActivity extends AppCompatActivity {
    Toolbar toolbar;
    DrawerLayout drawerLayout;
    TextView textView1,textView2,textView3,textView4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar=findViewById(R.id.toolbar);
        drawerLayout=findViewById(R.id.dra);
        textView1=findViewById(R.id.tv1);
        textView2=findViewById(R.id.tv2);
        textView3=findViewById(R.id.t3);
        textView4=findViewById(R.id.tv4);

        toolbar.setTitle("我是标题");
        toolbar.setTitleTextColor(Color.RED);
        toolbar.setSubtitle("我是子标题");
        toolbar.setSubtitleTextColor(Color.BLUE);

        toolbar.setNavigationIcon(R.mipmap.ic_launcher_round);
//        toolbar.setLogo(R.mipmap.b);
        toolbar.inflateMenu(R.menu.menu);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                 switch (menuItem.getItemId()){
                     case R.id.item1:
                         Toast.makeText(MainActivity.this, "菜单1", Toast.LENGTH_SHORT).show();
                         break;
                     case R.id.item2:
                         Toast.makeText(MainActivity.this, "菜单2", Toast.LENGTH_SHORT).show();
                         break;
                     case R.id.item3:
                         Toast.makeText(MainActivity.this, "菜单3", Toast.LENGTH_SHORT).show();
                         break;
                     case R.id.item4:
                         Toast.makeText(MainActivity.this, "菜单4", Toast.LENGTH_SHORT).show();
                         break;

                 }
                return false;
            }
        });
        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
                drawerLayout.closeDrawer(Gravity.LEFT);
            }
        });
        textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
                drawerLayout.closeDrawer(Gravity.LEFT);
            }
        });
        textView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
                drawerLayout.closeDrawer(Gravity.LEFT);
            }
        });
        textView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final PopupWindow popupWindow=new PopupWindow();
                popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
                popupWindow.setHeight(500);
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.popup_main, null);
                TextView textView=view.findViewById(R.id.tvvv);
                TextView textViewx=view.findViewById(R.id.tvv2);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
                textViewx.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                        drawerLayout.closeDrawer(Gravity.LEFT);
                    }
                });

                popupWindow.setContentView(view);

                popupWindow.showAtLocation(textView4,Gravity.BOTTOM,0,0);
            }
        });

//        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (drawerLayout.isDrawerOpen(Gravity.LEFT)){
//                    drawerLayout.closeDrawer(Gravity.LEFT);
//                }else {
//                    drawerLayout.openDrawer(Gravity.LEFT);
//                }
//            }
//        });

        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
        actionBarDrawerToggle.syncState();
        drawerLayout.addDrawerListener(actionBarDrawerToggle);

    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <android.support.v4.widget.DrawerLayout
            android:id="@+id/dra"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <LinearLayout
                android:id="@+id/zhu"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
            <android.support.v7.widget.Toolbar
                    android:background="#2FB776"
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="50dp">
                <TextView android:layout_width="60dp"
                          android:text="我的"
                          android:textSize="25dp"
                          android:textColor="#fff"
                          android:layout_height="wrap_content"/>
                <TextView android:layout_width="80dp"
                          android:text="音乐馆"
                          android:layout_marginLeft="25dp"
                          android:textSize="25dp"
                          android:textColor="#fff"
                          android:layout_height="wrap_content"/>
                <TextView android:layout_width="60dp"
                          android:text="发现"
                          android:layout_marginLeft="25dp"
                          android:textSize="25dp"
                          android:textColor="#fff"
                          android:layout_height="wrap_content"/>
                <TextView android:layout_width="60dp"
                          android:text="+"
                          android:layout_marginLeft="25dp"
                          android:textSize="25dp"
                          android:textColor="#fff"
                          android:layout_height="wrap_content"/>
            </android.support.v7.widget.Toolbar>
            <ImageView android:layout_width="match_parent"
                      android:gravity="center"
                       android:background="@mipmap/twoo"
                      android:layout_gravity="center"
                      android:layout_height="match_parent"/>

        </LinearLayout>
        <LinearLayout
                android:id="@+id/chouti"
                android:background="#B41E1E"
                android:layout_width="200dp"
                android:layout_gravity="left"
                android:layout_height="match_parent"
                android:orientation="vertical">
            <TextView android:layout_width="match_parent"
                      android:text="菜单1"
                      android:id="@+id/tv1"
                      android:textSize="20dp"
                      android:layout_height="50dp"/>
            <TextView android:layout_width="match_parent"
                      android:text="菜单2"
                      android:id="@+id/tv2"
                      android:textSize="20dp"
                      android:layout_height="50dp"/>
            <TextView android:layout_width="match_parent"
                      android:text="菜单3"
                      android:id="@+id/t3"
                      android:textSize="20dp"
                      android:layout_height="50dp"/>
            <TextView android:layout_width="match_parent"
                      android:text="关闭"
                      android:layout_gravity="bottom"
                      android:id="@+id/tv4"
                      android:textSize="20dp"
                      android:layout_height="50dp"/>

        </LinearLayout>

    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

//1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <android.support.v4.widget.DrawerLayout
            android:id="@+id/dra"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <LinearLayout
                android:id="@+id/zhu"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
            <android.support.v7.widget.Toolbar
                    android:background="#2FB776"
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="50dp">
                <TextView android:layout_width="60dp"
                          android:text="我的"
                          android:textSize="25dp"
                          android:textColor="#fff"
                          android:layout_height="wrap_content"/>
                <TextView android:layout_width="80dp"
                          android:text="音乐馆"
                          android:layout_marginLeft="25dp"
                          android:textSize="25dp"
                          android:textColor="#fff"
                          android:layout_height="wrap_content"/>
                <TextView android:layout_width="60dp"
                          android:text="发现"
                          android:layout_marginLeft="25dp"
                          android:textSize="25dp"
                          android:textColor="#fff"
                          android:layout_height="wrap_content"/>
                <TextView android:layout_width="60dp"
                          android:text="+"
                          android:layout_marginLeft="25dp"
                          android:textSize="25dp"
                          android:textColor="#fff"
                          android:layout_height="wrap_content"/>
            </android.support.v7.widget.Toolbar>
            <ImageView android:layout_width="match_parent"
                      android:gravity="center"
                       android:background="@mipmap/twoo"
                      android:layout_gravity="center"
                      android:layout_height="match_parent"/>

        </LinearLayout>
        <LinearLayout
                android:id="@+id/chouti"
                android:background="#B41E1E"
                android:layout_width="200dp"
                android:layout_gravity="left"
                android:layout_height="match_parent"
                android:orientation="vertical">
            <TextView android:layout_width="match_parent"
                      android:text="菜单1"
                      android:id="@+id/tv1"
                      android:textSize="20dp"
                      android:layout_height="50dp"/>
            <TextView android:layout_width="match_parent"
                      android:text="菜单2"
                      android:id="@+id/tv2"
                      android:textSize="20dp"
                      android:layout_height="50dp"/>
            <TextView android:layout_width="match_parent"
                      android:text="菜单3"
                      android:id="@+id/t3"
                      android:textSize="20dp"
                      android:layout_height="50dp"/>
            <TextView android:layout_width="match_parent"
                      android:text="关闭"
                      android:layout_gravity="bottom"
                      android:id="@+id/tv4"
                      android:textSize="20dp"
                      android:layout_height="50dp"/>

        </LinearLayout>

    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

//2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:background="#FFEB3B"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView android:layout_width="match_parent"
              android:id="@+id/tvvv"
              android:layout_gravity="center"
              android:gravity="center"
              android:drawableLeft="@mipmap/close"
              android:textSize="20dp"
              android:text="关闭QQ音乐\n关闭后QQ音乐停止本次运行"

              android:layout_height="100dp"/>
    <LinearLayout android:layout_width="match_parent" android:layout_height="1dp"></LinearLayout>
    <TextView android:layout_width="match_parent"
              android:id="@+id/tvv2"
              android:text="取消"
              android:textSize="20dp"
              android:gravity="center"
              android:layout_height="50dp"/>


</LinearLayout>
public class Main2Activity extends AppCompatActivity {

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

.SlideMenu实现抽屉
设置模式: setMode(SlidingMenu.LEFT);
设置触摸屏幕的模式:setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
//TOUCHMODE_FULLSCREEN全屏;TOUCHMODE_MARGIN边界;TOUCHMODE_NONE不能滑动
设置左侧菜单滑动显示的内容:slidingMenu.setMenu(R.layout.slide_menu);
设置左侧滑动菜单的阴影宽度:slidingMenu.setShadowWidth(300);
设置滑动时的渐变程度:slidingMenu.setFadeDegree(0.5f);范围0.0f-1.0f
设置淡入淡出的效果:slidingMenu.setFadeEnabled(true);
设置左侧滑动菜单的阴影图片(颜色):setShadowDrawable();
设置滑出时主页面显示的剩余宽度:slidingMenu.setBehindOffset(200);

导入第三方moudle:slidemenu
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    SlidingMenu slidingMenu;
    ImageView imageView;
    Frame1 frame1;Frame2 frame2;Frame3 frame3;
    TextView textView1,textView2,textView3;
    RadioButton radioButton1,radioButton2,radioButton3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView=findViewById(R.id.iv);
        frame1=new Frame1();
        frame2=new Frame2();
        frame3=new Frame3();
        radioButton1=findViewById(R.id.rab1);
        radioButton2=findViewById(R.id.rab2);
        radioButton3=findViewById(R.id.rab3);

        getSupportFragmentManager().beginTransaction().replace(R.id.frame,frame1).commit();
        radioButton1.setOnClickListener(this);
        radioButton2.setOnClickListener(this);
        radioButton3.setOnClickListener(this);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                slidingMenu.showMenu();
            }
        });

        slidingMenu =new SlidingMenu(this);
        slidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);
        slidingMenu.setMode(SlidingMenu.LEFT);

        View view = LayoutInflater.from(this).inflate(R.layout.sild_layout, null);
        textView1=view.findViewById(R.id.tv_one);
        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                radioButton1.setChecked(true);
                slidingMenu.showContent();
                getSupportFragmentManager().beginTransaction().replace(R.id.frame,frame1).commit();
            }
        });
        textView2=view.findViewById(R.id.tv_two);
        textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                radioButton2.setChecked(true);
                slidingMenu.showContent();
                getSupportFragmentManager().beginTransaction().replace(R.id.frame,frame2).commit();
            }
        });
        textView3=view.findViewById(R.id.tv_three);
        textView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                radioButton3.setChecked(true);
                slidingMenu.showContent();
                getSupportFragmentManager().beginTransaction().replace(R.id.frame,frame3).commit();
            }
        });
        slidingMenu.setMenu(view);


        slidingMenu.setShadowWidth(300);
        slidingMenu.setFadeDegree(0.5f);
        slidingMenu.setFadeEnabled(true);
//        slidingMenu.setShadowDrawable(R.mipmap.ic_launcher_round);


        slidingMenu.setBehindOffset(300);
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rab1:
                radioButton1.setChecked(true);
                getSupportFragmentManager().beginTransaction().replace(R.id.frame,frame1).commit();
                break;
            case R.id.rab2:
                radioButton2.setChecked(true);
                getSupportFragmentManager().beginTransaction().replace(R.id.frame,frame2).commit();
                break;
            case R.id.rab3:
                radioButton3.setChecked(true);
                getSupportFragmentManager().beginTransaction().replace(R.id.frame,frame3).commit();
                break;
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

   <android.support.v7.widget.Toolbar
           android:background="#FFC107"
           android:layout_width="match_parent"
           android:layout_height="50dp">
       <ImageView android:layout_width="50dp"
                  android:id="@+id/iv"
                  android:src="@mipmap/ic_launcher_round"
                  android:layout_height="50dp"/>

   </android.support.v7.widget.Toolbar>
    <FrameLayout
            android:id="@+id/frame"
            android:layout_weight="7"
            android:layout_width="match_parent"
                  android:layout_height="0dp">

    </FrameLayout>
    <RadioGroup
            android:layout_weight="1"
            android:layout_width="match_parent"
                android:orientation="horizontal"
                android:layout_height="0dp">
        <RadioButton android:layout_width="0dp"
                     android:layout_weight="1"
                     android:button="@null"
                     android:text="One"
                     android:checked="true"
                     android:textColor="@drawable/text"
                     android:id="@+id/rab1"
                     android:textSize="20dp"
                     android:gravity="center"
                     android:layout_height="match_parent"/>
        <RadioButton android:layout_width="0dp"
                     android:layout_weight="1"
                     android:button="@null"
                     android:text="Two"
                     android:textColor="@drawable/text"
                     android:id="@+id/rab2"
                     android:textSize="20dp"
                     android:gravity="center"
                     android:layout_height="match_parent"/>
        <RadioButton android:layout_width="0dp"
                     android:layout_weight="1"
                     android:button="@null"
                     android:text="Three"
                     android:textColor="@drawable/text"
                     android:id="@+id/rab3"
                     android:textSize="20dp"
                     android:gravity="center"
                     android:layout_height="match_parent"/>

    </RadioGroup>

</LinearLayout>

//1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:background="#9BA7F0"
              android:orientation="vertical"
              android:layout_height="match_parent">
    <TextView android:layout_width="match_parent"
              android:id="@+id/tv_one"
              android:text="ONE"
              android:textSize="20dp"
              android:gravity="center"
              android:layout_height="100dp"/>
    <TextView android:layout_width="match_parent"
              android:id="@+id/tv_two"
              android:text="TWO"
              android:textSize="20dp"
              android:gravity="center"
              android:layout_height="100dp"/>
    <TextView android:layout_width="match_parent"
              android:id="@+id/tv_three"
              android:text="THREE"
              android:textSize="20dp"
              android:gravity="center"
              android:layout_height="100dp"/>

</LinearLayout>

SurfaceView和View的区别
View 主要适用于主动更新的情况,而 surfaceView 主要适用于被动更新,例如频繁的刷新。
View 在主线程中对画面进行刷新,而 surfaceView 通常会通过一个子线程来进行页面的刷新
View 在绘图时没有使用双缓冲机制,而 surfaceView 在底层实现机制上就已经实现了双缓冲机制。
总结就是,如果你的自定义 View 需要频繁刷新,或者刷新时数据处理量很大,考虑用 SurfaceView 来替代 View。
双缓冲:
在这里插入图片描述
效果图
在这里插入图片描述

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback{
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    ArrayList<Mess> arrayList=new ArrayList<>();
    SurfaceView surfaceView1;
    SurfaceHolder surfaceHolder1;
    Button button;
    EditText editText;
    MediaPlayer mediaPlayer=new MediaPlayer();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        surfaceView = findViewById(R.id.surf);
        button=findViewById(R.id.btn);
        editText=findViewById(R.id.et);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);

        surfaceView1=findViewById(R.id.surf1);
        surfaceHolder1 = surfaceView1.getHolder();
        surfaceHolder1.addCallback(this);
        surfaceView.setZOrderOnTop(true);
        surfaceHolder.setFormat(PixelFormat.TRANSPARENT);

        mediaPlayer.reset();
        try {
      		  //要加上网络权限
            mediaPlayer.setDataSource("http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4");
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }


        arrayList.add(new Mess("嘻嘻嘻",0,(int) (Math.random() * (400-50+1)+100)));
        arrayList.add(new Mess("发达",0,(int) (Math.random() * (400-50+1)+100)));
        arrayList.add(new Mess("嘻嘻千万富翁而嘻",0,(int) (Math.random() * (400-50+1)+100)));
        arrayList.add(new Mess("是的哥哥",0,(int) (Math.random() * (400-50+1)+100)));
        arrayList.add(new Mess("嘻撒大声地翁热嘻嘻",0,(int) (Math.random() * (400-50+1)+100)));
        arrayList.add(new Mess("儿童团让人",0,(int) (Math.random() * (400-50+1)+100)));
        arrayList.add(new Mess("而有人干事",0,(int) (Math.random() * (400-50+1)+100)));



        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = String.valueOf(editText.getText());
                arrayList.add(new Mess(text,0,(int) (Math.random() * (400-50+1)+100)));
                editText.setText("");
            }
        });
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (holder==surfaceHolder1){
            mediaPlayer.setDisplay(holder);
        }else {
            new MyThread().start();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
    class MyThread extends Thread{
        @Override
        public void run() {
            super.run();

            Paint paint = new Paint();

            paint.setTextSize(50.5f);

            while(true){
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                int a = (int) (Math.random()*255);
                int b = (int) (Math.random()*255);
                int c = (int) (Math.random()*255);

                paint.setColor(Color.rgb(a,b,c));

                Canvas canvas = surfaceHolder.lockCanvas();
                if (canvas==null){
                    break;
                }
                canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
                for (int i = 0; i < arrayList.size(); i++) {

                    String message = arrayList.get(i).getMessage();
                    int x = arrayList.get(i).getX();
                    arrayList.get(i).setX(x+=10);
                    int y = arrayList.get(i).getY();
                    canvas.drawText(message,arrayList.get(i).getX(),y,paint);
                }

                surfaceHolder.unlockCanvasAndPost(canvas);
            }


        }
    }
}

public class Mess {
    private String message;
    private int x;
    private int y;

    public Mess(String message, int x, int y) {
        this.message = message;
        this.x = x;
        this.y = y;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
        <FrameLayout android:layout_width="match_parent"
                     android:layout_height="400dp">
                <SurfaceView android:layout_width="match_parent"
                             android:id="@+id/surf"
                             android:layout_height="400dp"/>
                <SurfaceView android:layout_width="match_parent"
                             android:id="@+id/surf1"
                             android:layout_height="400dp"/>
        </FrameLayout>

        <EditText android:layout_width="match_parent"
                  android:id="@+id/et"
                  android:layout_height="50dp"/>
        <Button android:layout_width="match_parent"
                android:text="发送"
                android:id="@+id/btn"
                android:layout_height="50dp"/>

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值