美文网首页
Android_开发_Day21_app滑动消衣(做为绅士的你确

Android_开发_Day21_app滑动消衣(做为绅士的你确

作者: 不要问我问就是百度 | 来源:发表于2019-08-25 21:45 被阅读0次

    Android_开发_Day21_app滑动消衣

    目的:

    理解app开发过程中需要的东西哪些是资源,哪些是代码,什么是配置文件,以及配置文件有什么用。

    技术:

    <1> 一个app分为几块:

    app首先需要有界面,那app的界面用什么搭建,在Android里面可以用两种方式,一种是xml文件来搭建,使用的语言也非常简单,和前面讲的HTML一样是标记语言,第二就是用java代码来搭建,这种方法很复杂而且还容易出现错误,因此一般不建议使用,但是有些场合如动态图就需要用Java来搭建,因为其灵活性很高,相反xml文件适合搭建静态界面。app的第二块就是代码了,代码是app的灵魂,灵活运用Java可以写出很多有趣的东西。第三就是资源,资源就包括整个程序中要用到的图片视频音频等。这些在Android studio里体现如下图:


    目录结构.jpg
    <2> 一个界面的生命周期:

    一个app运行起来系统会默认调用MainActivity类里面的onCreate()方法,而MainActivity必须继承AppCompatActivity,才能完成界面的创造,因此可以这么认为onCreate()就是main函数,程序的接入点,其实除了onCreate()方法还有很多方法,onStart,onResume,onPause,onDestroy,onRestart等,这些方法均是父类AppCompatActivity里面的方法,在app运行时系统会自动调用这些方法,具体什么时候调用跟你的操作相关,打开app时onCreate(),onResume(),onRestart()方法会被调用,切换到后台时onPause()方法会被调用,退出应用程序时onPause()和onDestroy()方法都会被调用。

    <3> 简单用xml添加一幅图片:

    由于xml时标记语言因此猜也知道用<开头,具体代码如下:

    <ImageView
            android:layout_width="图片的大小,可以是填满整个边框用match_parent,可以是包裹内容用wrap_content,可以自定大小xxxdp"
            android:layout_height="match_parent"
            android:src="@drawable/图片文件名,不可以用中文,并且图片要事先放到drawable文件夹下不然读取不到" />
    

    技术如何使用:

    完成一个简单的demo手指滑动消衣的游戏,代码如下:

    public class MainActivity extends AppCompatActivity {
    
        ImageView ivForeground;
        Bitmap org;
        Bitmap bitmap;
        Canvas canvas;
        Paint paint;
        Matrix matrix;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //找到图片
    
            ivForeground = findViewById(R.id.iv_fr);
            //将要操作的图片读取出来
            org = BitmapFactory.decodeResource(getResources(),R.drawable.p0);
            //拷贝一份图片
            //创建空位图
            bitmap = Bitmap.createBitmap(org.getWidth(),org.getHeight(),org.getConfig());
            //创建一个画布
            canvas = new Canvas(bitmap);
            //创建一个画笔
            paint = new Paint();
            //创建一个矩阵
            matrix = new Matrix();
            //旋转图片
            //matrix.setRotate(90,240,400);
            //移动图片
            //matrix.setTranslate(100,0);
            //反转
            //matrix.setScale(-1f,1f);
            //matrix.postTranslate(org.getWidth(),0);
            //画一幅图
            canvas.drawBitmap(org,matrix,paint);
            //显示图片
            ivForeground.setImageBitmap(bitmap);
            //给前景图片添加touch事件 当有触摸事件发生,系统就会接收并回调这个事件
            ivForeground.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    //获取当前事件
                    int action = motionEvent.getAction();
    
                    //判断状态
                    if (action == MotionEvent.ACTION_MOVE){
                        //获取触摸点的坐标
                        int x = (int) motionEvent.getX();
                        int y = (int) motionEvent.getY();
                        System.out.println(canvas);
                        //替换x,y对应的像素
                        for (int i = 0; i < 500 ; i++) {
                            for (int j = 0; j < 500; j++) {
                                bitmap.setPixel(x+i,y+j,Color.TRANSPARENT);
                            }
                        }
                        //canvas.drawBitmap(org,new Matrix(),paint);
    
                        ivForeground.setImageBitmap(bitmap);
                    }
                    return true;
                }
            });
        }
    

    xml文件的配置:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout 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=".MainActivity"
        android:id="@+id/fl_main">
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/p1" />
    
        <ImageView
            android:id="@+id/iv_fr"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/p0"/>
    
    </FrameLayout>
    

    总结:

    Android里面的app给人的感觉是问题太多了,比如说同一个app这台设备可能能运行,下一台设备就可能不能运行,同时也和性能有关,一张稍微大一点的图片,4K分辨率,一运行就卡闪退,不得不降低分辨率,安卓端有太多的不确定性了。

    相关文章

      网友评论

          本文标题:Android_开发_Day21_app滑动消衣(做为绅士的你确

          本文链接:https://www.haomeiwen.com/subject/ugjkectx.html