纯色图片颜色渐变动画

纯色图片颜色渐变动画的实现,主要有两点:

1、纯色图片颜色的改变,使用的方法为ImageView里的setColorFilter(int color);

2、动画使用ValueAnimator,配合相应的颜色TypeEvalutor。

代码实现如下:

public class ImageActivity extends AppCompatActivity {

    private ImageView imageView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
        imageView = findViewById(R.id.imageView);

        ValueAnimator valueAnimator=new ValueAnimator();
        valueAnimator.setIntValues(Color.RED,Color.GRAY);
        valueAnimator.setEvaluator(new ColorEvaluator());
        valueAnimator.setDuration(5000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                imageView.setColorFilter((Integer) animation.getAnimatedValue());
            }
        });
        valueAnimator.start();
    }

    private class ColorEvaluator implements TypeEvaluator{

        @Override
        public Object evaluate(float fraction, Object startValue, Object endValue) {
            int startInt = (Integer) startValue;
            float startA = ((startInt >> 24) & 0xff) / 255.0f;
            float startR = ((startInt >> 16) & 0xff) / 255.0f;
            float startG = ((startInt >>  8) & 0xff) / 255.0f;
            float startB = ( startInt        & 0xff) / 255.0f;

            int endInt = (Integer) endValue;
            float endA = ((endInt >> 24) & 0xff) / 255.0f;
            float endR = ((endInt >> 16) & 0xff) / 255.0f;
            float endG = ((endInt >>  8) & 0xff) / 255.0f;
            float endB = ( endInt        & 0xff) / 255.0f;

            // convert from sRGB to linear
            startR = (float) Math.pow(startR, 2.2);
            startG = (float) Math.pow(startG, 2.2);
            startB = (float) Math.pow(startB, 2.2);

            endR = (float) Math.pow(endR, 2.2);
            endG = (float) Math.pow(endG, 2.2);
            endB = (float) Math.pow(endB, 2.2);

            // compute the interpolated color in linear space
            float a = startA + fraction * (endA - startA);
            float r = startR + fraction * (endR - startR);
            float g = startG + fraction * (endG - startG);
            float b = startB + fraction * (endB - startB);

            // convert back to sRGB in the [0..255] range
            a = a * 255.0f;
            r = (float) Math.pow(r, 1.0 / 2.2) * 255.0f;
            g = (float) Math.pow(g, 1.0 / 2.2) * 255.0f;
            b = (float) Math.pow(b, 1.0 / 2.2) * 255.0f;

            return Math.round(a) << 24 | Math.round(r) << 16 | Math.round(g) << 8 | Math.round(b);
        }
    }
}

最后再上传一张纯色图片!



three.js是一个JavaScript库,用于在WebGL上创建复杂的3D图形。渐变色长方体在three.js中可以通过材质(Material)和纹理(Texture)来实现。首先,你需要创建一个立方体几何体(BoxGeometry),然后给它创建一个带渐变的材质,比如`THREE.GradientMaterial`,这个材质可以设置两个颜色渐变范围。 下面是一个简单的示例代码: ```javascript // 创建场景、相机和渲染器 const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); const renderer = new THREE.WebGLRenderer(); // 创建渐变材质 const gradientMaterial = new THREE.GradientMaterial({ colors: [0x00ff00, 0xff0000], // 绿色到红色的颜色渐变 map: null, // 可以选择加载图片作为纹理,这里直接用默认的纯色 }); // 创建长方体 const boxGeometry = new THREE.BoxGeometry(1, 1, 1); // 宽高长各为1 const cube = new THREE.Mesh(boxGeometry, gradientMaterial); scene.add(cube); // 设置相机位置并渲染 camera.position.z = 5; // 设置相机远离长方体 renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); function animate() { requestAnimationFrame(animate); cube.rotation.x += 0.01; cube.rotation.y += 0.01; renderer.render(scene, camera); } animate(); ``` 在这个例子中,长方体贴上了渐变材质,每次动画循环时,长方体会旋转展示不同的视角效果,可以看到颜色渐变效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值