Android绘图机制 Demo(简单完成美图秀秀的滤镜)

Posted 春招进大厂的梦想家

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android绘图机制 Demo(简单完成美图秀秀的滤镜)相关的知识,希望对你有一定的参考价值。

android绘图机制 Demo(简单完成美图秀秀的滤镜)

1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2" />

    <GridLayout
        android:id="@+id/grid_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:columnCount="5"
        android:rowCount="4"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_change"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="改变"/>
        <Button
            android:id="@+id/btn_reset"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="重置"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_gray"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="灰度"/>
        <Button
            android:id="@+id/btn_reverse"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="反转"/>
        <Button
            android:id="@+id/btn_nostalgia"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="怀旧"/>
        <Button
            android:id="@+id/btn_discoloration"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="去色"/>
        <Button
            android:id="@+id/btn_high_saturation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="饱和"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_negative"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="底片"/>
        <Button
            android:id="@+id/btn_old_image"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="老照片"/>
    </LinearLayout>

</LinearLayout>

2.java

package com.example.android_image;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.ImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {


    @BindView(R.id.image_view)
    ImageView imageView;
    @BindView(R.id.grid_layout)
    GridLayout gridLayout;
    @BindView(R.id.btn_change)
    Button btnChange;
    @BindView(R.id.btn_reset)
    Button btnReset;
    @BindView(R.id.btn_gray)
    Button btnGray;
    @BindView(R.id.btn_reverse)
    Button btnReverse;
    @BindView(R.id.btn_nostalgia)
    Button btnNostalgia;
    @BindView(R.id.btn_discoloration)
    Button btnDiscoloration;
    @BindView(R.id.btn_high_saturation)
    Button btnHighSaturation;
    @BindView(R.id.btn_negative)
    Button btnNegative;
    @BindView(R.id.btn_old_image)
    Button btnOldImage;

    private Bitmap bitmap;
    private int mEtWidth;
    private int mEtHeight;
    private EditText[] editTexts = new EditText[20];
    private float[] colorMatrices = new float[20];
 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
        imageView.setImageBitmap(bitmap);
        //无法在onCreate()方法中获得视图的宽高值,
        //所以通过View的post方法,在视图创建完毕后获得其宽高值
        gridLayout.post(new Runnable() {
            @Override
            public void run() {
                //获取宽高信息
                mEtWidth = gridLayout.getWidth() / 5;
                mEtHeight = gridLayout.getHeight() / 4;
                addEts();
                initMatrix();
            }
        });
    }

    //添加EditText
    private void addEts() {
        for (int i = 0; i < 20; i++) {
            EditText editText = new EditText(MainActivity.this);
            editTexts[i] = editText;
            gridLayout.addView(editText, mEtWidth, mEtHeight);
        }
    }

    //初始化颜色矩阵为初始状态
    private void initMatrix() {
        for (int i = 0; i < 20; i++) {
            if (i % 6 == 0) {
                editTexts[i].setText(String.valueOf(1));
            } else {
                editTexts[i].setText(String.valueOf(0));
            }
        }
    }


    //获取矩阵值
    private void getMatrix() {
        for (int i = 0; i < 20; i++) {
            colorMatrices[i] = Float.valueOf(editTexts[i].getText().toString());
        }
    }

    //将矩阵值设置到图像
    private void setImageMatrix() {
        Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Bitmap.Config.ARGB_8888);
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(colorMatrices);
        Canvas canvas = new Canvas(bmp);
        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        canvas.drawBitmap(bitmap, 0, 0, paint);
        imageView.setImageBitmap(bmp);
    }


    //特效(ColorMatrices)
    private void specialEffect(int type) {
        switch (type) {
            case 1://灰度
                colorMatrices = new float[]{0.33f, 0.59f, 0.11f, 0, 0,
                        0.33f, 0.59f, 0.11f, 0, 0,
                        0.33f, 0.59f, 0.11f, 0, 0,
                        0, 0, 0, 1, 0};
                break;
            case 2://反转
                colorMatrices = new float[]{-1, 0, 0, 1, 1,
                        0, -1, 0, 1, 1,
                        0, 0, -1, 1, 1,
                        0, 0, 0, 1, 0};
                break;
            case 3://怀旧
                colorMatrices = new float[]{0.393f, 0.769f, 0.189f, 0, 0,
                        0.349f, 0.686f, 0.168f, 0, 0,
                        0.272f, 0.534f, 0.131f, 0, 0,
                        0, 0, 0, 1, 0};
                break;
            case 4://去色
                colorMatrices = new float[]{1.5f, 1.5f, 1.5f, 0, -1,
                        1.5f, 1.5f, 1.5f, 0, -1,
                        1.5f, 1.5f, 1.5f, 0, -1,
                        0, 0, 0, 1, 0};
                break;
            case 5://高饱和度
                colorMatrices = new float[]{1.438f, -0.122f, -0.016f, 0, -0.03f,
                        -0.062f, 1.378f, -0.016f, 0, 0.05f,
                        -0.062f, -0.122f, 1.483f, 0, -0.02f,
                        0, 0, 0, 1, 0};
                break;
        }
        for (int i = 0; i < 20; i++) {
            editTexts[i].setText(String.valueOf(colorMatrices[i]));
        }
    }

    //底片(像素点)
    private Bitmap handleImageNegative(Bitmap bm) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        int color;
        int r, g, b, a;

        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        int[] oldPx = new int[width * height];
        int[] newPx = new int[width * height];
        bm.getPixels(oldPx, 0, width, 0, 0, width, height);
        for (int i = 0; i < width * height; i++) {
            color = oldPx[i];
            r = Color.red(color);
            g = Color.green(color);
            b = Color.blue(color);
            a = Color.alpha(color);

            r = 255 - r;
            g = 255 - g;
            b = 255 - b;

            if (r > 255) {
                r = 255;
            } else if (r < 0) {
                r = 0;
            }
            if (g 以上是关于Android绘图机制 Demo(简单完成美图秀秀的滤镜)的主要内容,如果未能解决你的问题,请参考以下文章

美图秀秀下载|美图秀秀手机版下载

打造自己的“美图秀秀”

美图秀秀一寸照片的制作

如何用美图秀秀更改照片背景颜色

安卓怎么用美图秀秀将图片的四个角做成的圆角?

怎么用美图秀秀将图片的四个角做成这样的圆角?