[转帖]Android软件开发之盘点自定义View界面大合集(二)_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2932 | 回复: 0   主题: [转帖]Android软件开发之盘点自定义View界面大合集(二)        下一篇 
wei.wang
注册用户
等级:少校
经验:1001
发帖:87
精华:0
注册:2013-8-29
状态:离线
发送短消息息给wei.wang 加好友    发送短消息息给wei.wang 发消息
发表于: IP:您无权察看 2013-9-9 9:42:29 | [全部帖] [楼主帖] 楼主

  1. package cn.m15.xys;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Matrix;
  8. import android.graphics.Paint;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. import android.widget.LinearLayout;
  14. public class Image extends Activity {
  15.        ImageView imageView = null;
  16.       
  17.        @Override
  18.        protected void onCreate(Bundle savedInstanceState) {
  19.              imageView = new ImageView(this);
  20.              setContentView(R.layout.image);
  21.              LinearLayout ll = (LinearLayout) findViewById(R.id.iamgeid);
  22.              ll.addView(imageView);
  23.              // 向左移动
  24.              Button botton0 = (Button) findViewById(R.id.buttonLeft);
  25.              botton0.setOnClickListener(new OnClickListener() {
  26.                    @Override
  27.                    public void onClick(View arg0) {
  28.                          imageView.setPosLeft();
  29.                    }
  30.              });
  31.             
  32.              // 向右移动
  33.              Button botton1 = (Button) findViewById(R.id.buttonRight);
  34.              botton1.setOnClickListener(new OnClickListener() {
  35.                    @Override
  36.                    public void onClick(View arg0) {
  37.                          imageView.setPosRight();
  38.                    }
  39.              });
  40.              // 左旋转
  41.              Button botton2 = (Button) findViewById(R.id.buttonRotationLeft);
  42.              botton2.setOnClickListener(new OnClickListener() {
  43.                    @Override
  44.                    public void onClick(View arg0) {
  45.                          imageView.setRotationLeft();
  46.                    }
  47.              });
  48.             
  49.              // 右旋转
  50.              Button botton3 = (Button) findViewById(R.id.buttonRotationRight);
  51.              botton3.setOnClickListener(new OnClickListener() {
  52.                    @Override
  53.                    public void onClick(View arg0) {
  54.                          imageView.setRotationRight();
  55.                    }
  56.              });
  57.             
  58.              // 缩小
  59.              Button botton4 = (Button) findViewById(R.id.buttonNarrow);
  60.              botton4.setOnClickListener(new OnClickListener() {
  61.                   
  62.                    @Override
  63.                    public void onClick(View arg0) {
  64.                          imageView.setNarrow();
  65.                    }
  66.              });
  67.             
  68.              // 放大
  69.              Button botton5 = (Button) findViewById(R.id.buttonEnlarge);
  70.              botton5.setOnClickListener(new OnClickListener() {
  71.                   
  72.                    @Override
  73.                    public void onClick(View arg0) {
  74.                          imageView.setEnlarge();
  75.                    }
  76.              });
  77.             
  78.              super.onCreate(savedInstanceState);
  79.             
  80.        }
  81.       
  82.        class ImageView extends View {
  83.              Paint mPaint = null;
  84.              Bitmap bitMap = null;
  85.              Bitmap bitMapDisplay = null;
  86.              int m_posX = 120;
  87.              int m_posY = 50;
  88.              int m_bitMapWidth = 0;
  89.              int m_bitMapHeight = 0;
  90.              Matrix mMatrix = null;
  91.              float mAngle = 0.0f;
  92.              float mScale = 1f;//1为原图的大小
  93.             
  94.              public ImageView(Context context) {
  95.                    super(context);
  96.                    mPaint = new Paint();
  97.                    mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
  98.                    bitMap = BitmapFactory.decodeResource(this.getResources(),
  99.                    R.drawable.image);
  100.                    bitMapDisplay = bitMap;
  101.                    mMatrix = new Matrix();
  102.                    // 获取图片宽高
  103.                    m_bitMapWidth = bitMap.getWidth();
  104.                    m_bitMapHeight = bitMap.getHeight();
  105.              }
  106.             
  107.              // 向左移动
  108.              public void setPosLeft() {
  109.                    m_posX -= 10;
  110.              }
  111.             
  112.              // 向右移动
  113.              public void setPosRight() {
  114.                    m_posX += 10;
  115.              }
  116.             
  117.              // 向左旋转
  118.              public void setRotationLeft() {
  119.                    mAngle--;
  120.                    setAngle();
  121.              }
  122.             
  123.              // 向右旋转
  124.              public void setRotationRight() {
  125.                    mAngle++;
  126.                    setAngle();
  127.              }
  128.             
  129.              // 缩小图片
  130.              public void setNarrow() {
  131.                    if (mScale > 0.5) {
  132.                          mScale -= 0.1;
  133.                          setScale();
  134.                    }
  135.              }
  136.             
  137.              // 放大图片
  138.              public void setEnlarge() {
  139.                    if (mScale < 2) {
  140.                          mScale += 0.1;
  141.                          setScale();
  142.                    }
  143.              }
  144.             
  145.              // 设置缩放比例
  146.              public void setAngle() {
  147.                    mMatrix.reset();
  148.                    mMatrix.setRotate(mAngle);
  149.                    bitMapDisplay = Bitmap.createBitmap(bitMap, 0, 0, m_bitMapWidth,
  150.                    m_bitMapHeight, mMatrix, true);
  151.              }
  152.             
  153.              // 设置旋转比例
  154.              public void setScale() {
  155.                    mMatrix.reset();
  156.                    //float sx X轴缩放 
  157.                    //float sy Y轴缩放
  158.                    mMatrix.postScale(mScale, mScale);
  159.                    bitMapDisplay = Bitmap.createBitmap(bitMap, 0, 0, m_bitMapWidth,
  160.                    m_bitMapHeight, mMatrix, true);
  161.              }
  162.             
  163.              @Override
  164.              protected void onDraw(Canvas canvas) {
  165.                    super.onDraw(canvas);
  166.                    canvas.drawBitmap(bitMapDisplay, m_posX, m_posY, mPaint);
  167.                    invalidate();
  168.              }
  169.        }
  170. }

复制代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:id="@+id/iamgeid"
  4.     android:orientation="vertical"
  5.     android:layout_width="fill_parent"
  6.     android:layout_height="fill_parent"
  7.     >
  8.         <Button android:id="@+id/buttonLeft"
  9.             android:layout_width="fill_parent" android:layout_height="wrap_content"
  10.             android:text="图片向左移动"
  11.             />
  12.         <Button android:id="@+id/buttonRight"
  13.             android:layout_width="fill_parent" android:layout_height="wrap_content"
  14.             android:text="图片向右移动"
  15.             />
  16.         <Button android:id="@+id/buttonRotationLeft"
  17.             android:layout_width="fill_parent" android:layout_height="wrap_content"
  18.             android:text="图片左旋转"
  19.             />
  20.         <Button android:id="@+id/buttonRotationRight"
  21.             android:layout_width="fill_parent" android:layout_height="wrap_content"
  22.             android:text="图片右旋转"
  23.             />
  24.         <Button android:id="@+id/buttonNarrow"
  25.             android:layout_width="fill_parent" android:layout_height="wrap_content"
  26.             android:text="图片缩小"
  27.             />
  28.         <Button android:id="@+id/buttonEnlarge"
  29.             android:layout_width="fill_parent" android:layout_height="wrap_content"
  30.             android:text="图片放大"
  31.             />
  32. </LinearLayout>

复制代码


4.播放frame动画

        做游戏的话播放动画可就是必不可少的元素 帧动画帧动画 顾名思义是一帧一帧的播放 。 实际在开发中为了节省内存美术会把人物的图片切成一小块一小块然后由程序根据编辑器生成的点把图片在拼接起来这样就可以做到用更少的图片去实现更多的动画效果因为不太方便介绍图片编辑器 这个demo我只给大家简单的介绍一下播放动画的原理 后期我会深入���解。
              如图所示这个小人一直在行走 实际上是4张图片在来回切换 每张图片延迟500毫秒 后播下一张 以此类推。

北京联动北方科技有限公司

  1. package cn.m15.xys;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Color;
  8. import android.graphics.Paint;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. public class FramAnimation extends Activity {
  12.        public final static int ANIM_COUNT = 4;
  13.       
  14.        @Override
  15.        protected void onCreate(Bundle savedInstanceState) {
  16.              setContentView(new FramView(this));
  17.              super.onCreate(savedInstanceState);
  18.             
  19.        }
  20.       
  21.        class FramView extends View {
  22.              Bitmap[] bitmap = new Bitmap[ANIM_COUNT];
  23.              Bitmap display = null;
  24.              Paint paint = null;
  25.              long startTime = 0;
  26.              int playID = 0;
  27.             
  28.              public FramView(Context context) {
  29.                    super(context);
  30.                    for (int i = 0; i < ANIM_COUNT; i++) {
  31.                          bitmap[i] = BitmapFactory.decodeResource(this.getResources(),
  32.                          R.drawable.hero_a + i);
  33.                    }
  34.                    display = bitmap[0];
  35.                    paint = new Paint();
  36.                    startTime = System.currentTimeMillis();
  37.              }
  38.             
  39.              @Override
  40.              protected void onDraw(Canvas canvas) {
  41.                    super.onDraw(canvas);
  42.                    paint.setColor(Color.WHITE);
  43.                    canvas.drawText("播放动画中...", 100, 30, paint);
  44.                    long nowTime = System.currentTimeMillis();
  45.                    if (nowTime - startTime >= 500) {
  46.                          startTime=nowTime;
  47.                          playID++;
  48.                          if (playID >= ANIM_COUNT) {
  49.                                playID = 0;
  50.                          }
  51.                          canvas.drawBitmap(bitmap[playID], 100, 100, paint);
  52.                    }
  53.                    invalidate();
  54.              }
  55.        }
  56.       
  57. }




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论