在Android中2D中实现对图片的倒影

Linux大全评论205 views阅读模式

package com.joe.bitmap;

import Android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Bitmap.Config;
import android.util.Log;

public class BitmapUtils {
    private static final String TAG = "BitmapUtils";

    /**
     * Set the pixels's alpha. alpha's value decrease by the bit's height
     * @param pixels
     *            the array which contains pixels.
     * @param srcWidth
     *            the bitmap's width
     * @param srcHeight
     *            the bitmap's height
     * @param alphastart
     *            the first pixels's alpha
     * @param percent
     *            the line's fouction's slope (y = mx+b;)
     *            such as: alphastart=0x2fffffff;(alpha's value 0x2f)
     *             m = 0x2f/(height * percent) (if the shadow fill full of the
     *            bitmap,k =1. otherwise the shadow only in a half of the bitmap
     *            percent = 0.5) alpha = m*height + alphaLagest
     *            The percent max value are 1!
     * @param b
     *            the line's fouction's const (y = mx+b)
     */
    public static void shadowFromPixels(int[] pixels, int srcWidth,
        int srcHeight, int alphastart, float percent) {
        int b = alphastart;
        //if need display a  part of (such as 1/2,1/3...) bitmap
        int alpahLine = (int) (srcHeight * percent);
       
        float temp = (alphastart >>> 24);
        percent = ((temp / (srcHeight * percent)));
        int alpha = 0;
       
        Log.d(TAG, "shadowByPixels: percent=" + percent + "\tb=" + b
                + "\t temp=" + temp + "\tsrcHeight=" + srcHeight);
       
        for (int i = 0; i < srcHeight; i++) {
            if(i < alpahLine){
                alpha = b - (((int) (i * percent)) << 24);
            }else{
                alpha = 0x00ffffff;
            }
            for (int j = 0; j < srcWidth; j++) {
                pixels[j + i * srcWidth] = pixels[j + i * srcWidth] & alpha;
            }
        }
    }

    /**
     * Set the pixels's alpha. alpha's value decrease by the bit's height
     *
     * @param pixels the array which contains pixels.
     * @param srcWidth  the bitmap's width
     * @param srcHeight the bitmap's height
     * @param alphastart the first pixels's alpha
     */
    public static void shadowFromPixels(int[] pixels, int srcWidth,
            int srcHeight, int alphastart) {
            int b = alphastart;
            float temp = (alphastart >>> 24);
            float percent = (temp / srcHeight) ;
            int alpha = 0;
            Log.d(TAG, "shadowByPixels: percent=" + percent + "\tb=" + b
                    + "\t temp=" + temp + "\tsrcHeight=" + srcHeight);
            for (int i = 0; i < srcHeight; i++) {
                alpha = b -(((int) (i * percent)) << 24);
                for (int j = 0; j < srcWidth; j++) {
                    pixels[j + i * srcWidth] = pixels[j + i * srcWidth] & alpha;
                }
            }
        }
   
//我的这个 算法有点问题:
   我只考虑到 图片中每一行的alpha值是相同的,大多数图片是这样,但是有些图片不是哦.
   这里是把每一行的alpha值,重新设置成了一个值,这是不能通用的.
通用的做法是把 这行的alpha值取出来,乘以一个比例
当前行号 = a,(0~a总)
需要显示总的行号 = a总

企鹅博客
  • 本文由 发表于 2020年8月31日 16:37:16
  • 转载请务必保留本文链接:https://www.qieseo.com/148392.html

发表评论