12.11 阅读android项目源码笔记-水波view,左右翻页三种实现,图片、文件加密

水波vie:

class PlasmaView extends View implements View.OnTouchListener{  
    private Bitmap mBitmap;
    long time;
    long fps;

    public PlasmaView(Context context) {
        super(context);        
        Bitmap bmp = BitmapFactory.decodeResource(this.getResources(),R.drawable.mm);
        mBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.RGB_565);
        AnimRender.setBitmap(bmp);
        this.setOnTouchListener(this);
    }

    @Override 
    protected void onDraw(Canvas canvas) {
        long ct = System.currentTimeMillis();
        if(ct - time > 1000){

            time = ct;
            fps = 0;
        }
        fps++;      

        AnimRender.render(mBitmap);     
        canvas.drawBitmap(mBitmap, 0, 0, null);
        postInvalidate();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        AnimRender.drop((int)event.getX(), (int)event.getY(), 500);
        return false;
    }
}


class AnimRender{
    public static native void setBitmap(Bitmap src);
    public static native void render(Bitmap dst);
    public static native void drop(int x, int y, int height);

    static {
        System.loadLibrary("plasma");
    }
}

需要文件:/armeabi/libplasma.so

左右翻页三种实现

1.FlingGallery(网上下载 )

mGallery = new FlingGallery(this);
        mGallery.setPaddingWidth(5);
        mGallery.setAdapter(....)
mGallery.setIsGalleryCircular(mCheckBox.isChecked());//允许无限翻页?

2.viewpager
没什么好写的

3.ViewFlipper+手势

    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        return mGestureDetector.onTouchEvent(event);
    }

        /* * 用户按下触摸屏、快速移动后松开即触发这个事件 e1:第1个ACTION_DOWN MotionEvent e2:最后一个ACTION_MOVE * MotionEvent velocityX:X轴上的移动速度,像素/秒 velocityY:Y轴上的移动速度,像素/秒 触发条件 : * X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒 */
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
                && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
            // 当像左侧滑动的时候
            // 设置View进入屏幕时候使用的动画
            System.out.println("onFling");
            flipper.setInAnimation(inFromRightAnimation());
            // 设置View退出屏幕时候使用的动画
            flipper.setOutAnimation(outToLeftAnimation());
            flipper.showNext();
        } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
                && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
            // 当像右侧滑动的时候
            System.out.println("onFling1");
            flipper.setInAnimation(inFromLeftAnimation());
            flipper.setOutAnimation(outToRightAnimation());
            flipper.showPrevious();
        }
        return false;
    }

图片、文件加密

    //AES加密使用的秘钥,注意的是秘钥的长度必须是16位
    private static final String AES_KEY = "MyDifficultPassw";
    //混入的字节
    private static final String BYTE_KEY = "MyByte";

     /** * 混入字节加密 */
    public  void addByte(){
        try {
            //获取图片的字节流
            Bitmap bitmap = BitmapFactory.decodeFile(filePath);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] bytes = baos.toByteArray();
            FileOutputStream fops = new FileOutputStream(bytePath);
            //混入的字节流
            byte[] bytesAdd = BYTE_KEY.getBytes();
            fops.write(bytesAdd);
            fops.write(bytes);
            fops.flush();
            fops.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** * 移除混入的字节解密图片 */
    public  void removeByte(){
        try {
            FileInputStream stream = null;
            stream = new FileInputStream(new File(bytePath));
            ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
            byte[] b = new byte[1024];
            int n;
            int i=0;
            while ((n = stream.read(b)) != -1) {
                if(i==0){
                    //第一次写文件流的时候,移除我们之前混入的字节
                    out.write(b, BYTE_KEY.length(), n-BYTE_KEY.length());
                }else{
                    out.write(b, 0, n);
                }
                i++;
            }
            stream.close();
            out.close();
            //获取字节流显示图片
            byte[] bytes= out.toByteArray();
            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            img.setImageBitmap(bitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** * 使用AES加密标准进行加密 */
    public void aesEncrypt()  {
        try {
            FileInputStream fis = null;
            fis = new FileInputStream(filePath);
            FileOutputStream fos = new FileOutputStream(outPath);
            //SecretKeySpec此类来根据一个字节数组构造一个 SecretKey
            SecretKeySpec sks = new SecretKeySpec(AES_KEY.getBytes(),
                    "AES");
            //Cipher类为加密和解密提供密码功能,获取实例
            Cipher cipher = Cipher.getInstance("AES");
            //初始化
            cipher.init(Cipher.ENCRYPT_MODE, sks);
            //CipherOutputStream 为加密输出流
            CipherOutputStream cos = new CipherOutputStream(fos, cipher);
            int b;
            byte[] d = new byte[1024];
            while ((b = fis.read(d)) != -1) {
                cos.write(d, 0, b);
            }
            cos.flush();
            cos.close();
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /** * 使用AES标准解密 */
    public void aesDecrypt() {
        try {
            FileInputStream fis = null;
            fis = new FileInputStream(outPath);
            ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
            SecretKeySpec sks = new SecretKeySpec(AES_KEY.getBytes(),
                    "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, sks);
            //CipherInputStream 为加密输入流
            CipherInputStream cis = new CipherInputStream(fis, cipher);
            int b;
            byte[] d = new byte[1024];
            while ((b = cis.read(d)) != -1) {
                out.write(d, 0, b);
            }
            out.flush();
            out.close();
            cis.close();
            //获取字节流显示图片
            byte[] bytes= out.toByteArray();
            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            img.setImageBitmap(bitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

你可能感兴趣的:(源码,android)