Api Demo - .graphics(4)

//关键字:将对象(Drawable,Bitmap,Movie画到CANVAS, 取像素点。

package com.example.android.apis.graphics;

import com.example.android.apis.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.*;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;

public class BitmapDecode extends GraphicsActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));
    }
    
    private static class SampleView extends View {
        private Bitmap mBitmap;
        private Bitmap mBitmap2;
        private Bitmap mBitmap3;
        private Bitmap mBitmap4;
        private Drawable mDrawable;
        
        private Movie mMovie;
        private long mMovieStart;
        
        private static byte[] streamToBytes(InputStream is) {
            ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
            byte[] buffer = new byte[1024];
            int len;
            try {
                while ((len = is.read(buffer)) >= 0) {读取转换完成返回-1
                    os.write(buffer, 0, len);
                }
            } catch (java.io.IOException e) {
            }
            return os.toByteArray();
        }
        
        public SampleView(Context context) {
            super(context);
            setFocusable(true);
            
            java.io.InputStream is;
            is = context.getResources().openRawResource(R.drawable.beach);
            
            BitmapFactory.Options opts = new BitmapFactory.Options();
            Bitmap bm;


            /*
             *  设置inJustDecodeBounds为true后,decodeFile并不分配空间,
             *  但可计算出原始图片的长度和宽度,options.outWidth和options.outHeight。
             *  要对图片进行缩放,最大的问题就是怎么在运行时动态的改变inSampleSize的值,
             *  通过上面的inJustDecodeBounds可以知道图片原始的大小,
             *  那么这样以来就可以通过算法来得到一个恰当的inSampleSize值。
               */ 
            opts.inJustDecodeBounds = true;
            bm = BitmapFactory.decodeStream(is, null, opts);            
            // now opts.outWidth and opts.outHeight are the dimension of the
            // bitmap, even though bm is null
            
            opts.inJustDecodeBounds = false;    /
            opts.inSampleSize = 4;             // /inSampleSize是以2的指数的倒数倍进行放缩
            bm = BitmapFactory.decodeStream(is, null, opts);
            
            mBitmap = bm;
            
            // decode an image with transparency
            is = context.getResources().openRawResource(R.drawable.frog);
            mBitmap2 = BitmapFactory.decodeStream(is);
            
            // create a deep copy of it using getPixels() into different configs
            int w = mBitmap2.getWidth();
            int h = mBitmap2.getHeight();
            int[] pixels = new int[w*h];
            mBitmap2.getPixels(pixels, 0, w, 0, 0, w, h);//取像素点
            mBitmap3 = Bitmap.createBitmap(pixels, 0, w, w, h,
                                           Bitmap.Config.ARGB_8888);
            mBitmap4 = Bitmap.createBitmap(pixels, 0, w, w, h,
                                           Bitmap.Config.ARGB_4444);
            
            mDrawable = context.getResources().getDrawable(R.drawable.button);
            mDrawable.setBounds(150, 20, 300, 100);
            
            is = context.getResources().openRawResource(R.drawable.animated_gif);
            if (true) {
                mMovie = Movie.decodeStream(is);    //动态图GIF的获取
            } else {
                byte[] array = streamToBytes(is);
                mMovie = Movie.decodeByteArray(array, 0, array.length);
            }
        }
        
        @Override protected void onDraw(Canvas canvas) {
            canvas.drawColor(0xFFCCCCCC);            
            
            Paint p = new Paint();
            p.setAntiAlias(true);
            
            canvas.drawBitmap(mBitmap, 10, 10, null);
            canvas.drawBitmap(mBitmap2, 10, 170, null);
            canvas.drawBitmap(mBitmap3, 110, 170, null);
            canvas.drawBitmap(mBitmap4, 210, 170, null);
            
           mDrawable.draw(canvas);//CANVAS上画Drawable
            
            long now = android.os.SystemClock.uptimeMillis();
            if (mMovieStart == 0) {   // first time
                mMovieStart = now;
            }
            if (mMovie != null) {);//CANVAS上画GIF

                int dur = mMovie.duration();
                if (dur == 0) {
                    dur = 1000;
                }
                int relTime = (int)((now - mMovieStart) % dur);
                mMovie.setTime(relTime);
                mMovie.draw(canvas, getWidth() - mMovie.width(),
                            getHeight() - mMovie.height());
                invalidate();
            }
        }
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值