• 安卓自定义view_GDI绘图 _2d绘图_canvas绘图


    
    
    2014年到2016年 发生了很多事情,如今已成定局,现在想忘掉这些烦恼的事情,找点以前想干没有干的事情来做,塞满大脑就不去想了。
    之前,一直想做一款挂机类游戏,各种平台和开发语言都选择过了,从html5到C,C++,C#,unity3d各种小demo写了不少,最后还是选择了安卓平台来做....

    网上看了很多列子,关于自定义view的也就是类似于WIN 下用SDK 开发窗体了,发现安卓上实现这些很绕,那些列子都是要去继承那个什么surfaceview 一大堆乱七八糟的东西..

    在WIN下做开发很多年 习惯了WIN下的思维 不习惯那样搞 于是有了下面的代码



    package
    com.example.test; import java.io.IOException; import android.os.Bundle; import android.os.Message; import android.app.Activity; import android.content.pm.ActivityInfo; import android.view.Menu; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.os.Handler; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); // 隐去电池等图标和一切修饰部分(状态栏部分) this.requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); final MyView my = new MyView(this); setContentView(my); final Handler handler = new Handler() { public void handleMessage(Message message) { if (message.what == 0x123) { my.invalidate(); } } }; Thread r = new Thread() { public void run() { while (true) { handler.sendEmptyMessage(0x123); } } }; // Thread t = new Thread(r); r.start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
    package com.example.test;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    import android.content.Context;
    
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.PorterDuff.Mode;
    import android.graphics.Rect;
    import android.graphics.RectF;
    
    import android.os.Handler;
    import android.os.Message;
    import android.util.Log;
    
    import android.view.View;
    
    import android.widget.Button;
    import android.content.res.Resources;
    import android.content.res.Resources.Theme;
    
    public class MyView extends View {
    
        private Bitmap bmp;
        private Bitmap bmp2;
        private int ofs = 5;
        private Paint paint;
    
        public MyView(Context context) {
            super(context);
    
            Resources res = this.getResources();
            InputStream is = null;
            try {
                is = getResources().getAssets().open("bg.png");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            bmp = BitmapFactory.decodeStream(is);
            bmp2 = BitmapFactory.decodeResource(res, R.drawable.ic_launcher);
    
        }
    
        public void draw(Canvas canvas) {
            try {
                canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
                // canvas.drawPaint(null);
                canvas.drawBitmap(bmp, 0, 0, null);
                canvas.drawBitmap(bmp2, ofs, 0, null);
    
                // canvas.drawText(ofs, 100, 100, paint);// 画文字文本
    
                ofs++;
            } catch (Exception e) {
                Log.i(e.getMessage().toString(), null);
                // TODO: handle exception
            }
    
        }
    
    }

     更新::

    之前的思路是把继承于view的类作为画布,存在的问题是什么呢?不能在这个view画布中添加button控件 因为 button也是集成于view

    改下思路 

    Activity 舞台

    继承于view的类为精灵

    按照这个思路百度下 view 的属性 和 方法..就可以完成很多效果了


  • 相关阅读:
    2.7OpenIdConnectHandler 【RemoteAuthenticationHandler、IAuthenticationSignOutHandler】
    2.6OAuthHandler【RemoteAuthenticationHandler】
    2.0AuthenticationHandler【IAuthenticationHandler 】
    2.5RemoteAuthenticationHandler【AuthenticationHandler、IAuthenticationRequestHandler】
    2.4JwtBearerHandler 【AuthenticationHandler】
    在Centos7上安装Nominatim
    .net core使用 ELK
    linux 韩顺平课程笔记 3.11包管理工具(RPM和YUM)
    linux 韩顺平课程笔记 3.10进程管理
    linux 韩顺平课程笔记 3.5实用指令
  • 原文地址:https://www.cnblogs.com/cfas/p/5150135.html
Copyright © 2020-2023  润新知