android滤镜效果

1 篇文章 0 订阅
1 篇文章 0 订阅

##android滤镜效果的实现
代码具体实现:

public class MYMainActivity extends Activity implements View.OnClickListener{//,AdapterView.OnItemClickListener
    private Bitmap bitmap;
    private ImageView ivPhoto;
    private GridView matrixlayout;
    int mEtWidth;
    int mEtHeight;
    String postionString ;
    int mpostion;
    float[] mColormatrix =new  float[20];
    String[] stringname = new String[20];
    MySelectPic mySelectPic;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mymain);

        ivPhoto = findViewById(R.id.iv_photo);
        matrixlayout = findViewById(R.id.matrix_lauout);

        Button btn_chang = findViewById(R.id.btn_chang);
        Button btn_reset = findViewById(R.id.btn_reset);
        btn_chang.setOnClickListener(this);
        btn_reset.setOnClickListener(this);
        bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.iv_model);
        ivPhoto.setImageBitmap(bitmap);
        //这个方法可以在视图加载完,获取view的宽高
     /*   matrixlayout.post(new Runnable() {
            @Override
            public void run() {
                mEtWidth = matrixlayout.getWidth()/5;
                mEtHeight = matrixlayout.getHeight()/4;
                //addEidt();

            }
        });*/
        //初始化颜色矩阵
        initMatrit();
    }

    private void initMatrit() {
        for(int i = 0;i <20 ;i++){
            if (i%6 == 0){
               // mEts[i].setText(String.valueOf(1));
                stringname[i] = String.valueOf(1);
            }else{
              //  mEts[i].setText(String.valueOf(0));
                stringname[i] = String.valueOf(0);
            }

        }
        mySelectPic = new MySelectPic(this,stringname);
        matrixlayout.setAdapter(mySelectPic);
        //matrixlayout.setOnItemClickListener(this );
        mySelectPic.setOnChangeText(new MySelectPic.OnChangeText() {
            @Override
            public void OnChang(String s, int postion) {
                postionString = s;
                mpostion = postion;
                Toast.makeText(MYMainActivity.this,"postionString-->"+postionString+"mpostion-->"+mpostion,Toast.LENGTH_SHORT).show();
            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case  R.id.btn_chang:
                //作用矩阵效果
                getMatrix();//获取矩阵值
                setImageMatrix();//将矩阵设置到图像
                break;
            case  R.id.btn_reset:
                //重置矩阵效果
                break;

        }

    }
    private  void getMatrix(){
        for (int i = 0;i < 20;i++) {
            //String text = (String) mySelectPic.getItem(i);
            if (i == mpostion) {
                try {
                    mColormatrix[i] =  Float.valueOf(postionString);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            } else{

               // Log.e("i == mpostion","i == mpostion:"+mColormatrix[i]);
                String matrix = stringname[i];
                boolean isNone = matrix == null || "".equals(matrix);
                try {
                    mColormatrix[i] = isNone ? 0.0f : Float.valueOf(matrix);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
         	}
        }
    }
    //将矩阵设置到图像
    private void setImageMatrix(){
        if(bitmap !=null) {
            Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
            ColorMatrix colorMatrix = new ColorMatrix();
            colorMatrix.set(mColormatrix);
            for (int i = 0;i <mColormatrix.length;i++){
                Log.e("setImageMatrix","mColormatrix:"+mColormatrix[i]);
            }
            Canvas canvas = new Canvas(bmp);
            Paint paint = new Paint();
            paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

            canvas.drawBitmap(bitmap, 0, 0, paint);
            ivPhoto.setImageBitmap(bmp);
        }

    }
	
  /*  @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mpostion= position;
        EditText et =(EditText) view;
        postionString =  et.getText().toString();

    }*/
}
public class MySelectPic extends BaseAdapter {
    Context context ;
    String[] strings = new  String[20];
    EditText editText;
    public OnChangeText onChangeText;
    public MySelectPic(Context context,String[] strings){
        this.context = context;
        this.strings = strings;
    }
    @Override
    public int getCount() {
        return strings.length;
    }
    @Override
    public Object getItem(int position) {
        return editText;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
       
            convertView = View.inflate(context, R.layout.ed_layout, null);
          
            editText = convertView.findViewById(R.id.et);
            Log.e("getView",":"+strings[position]);
            editText.setText(strings[position]);
             editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                onChangeText.OnChang( s.toString(),position);//接口方法实现传值

            }
        });
        return convertView;
    }
   public void setOnChangeText(OnChangeText onChangeText){
        this.onChangeText = onChangeText;
    }
    public interface OnChangeText {
        void OnChang(String s,int postion);
    }

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
        <ImageView
            android:layout_width="300dp"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:id="@+id/iv_photo"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/iv_model"/>
<!--        <GridView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="4"
            android:id="@+id/matrix_lauout"
            android:columnCount="5"
            android:rowCount="4">

        </GridView>-->
        <GridView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="4"
            android:id="@+id/matrix_lauout"
            android:columnWidth="80dp"
            android:stretchMode="spacingWidthUniform"
            android:numColumns="5"
            />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
                <Button
                    android:layout_weight="1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/btn_chang"
                    android:text="change"
                    />
                <Button
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:id="@+id/btn_reset"
                    android:text="reset"/>
        </LinearLayout>

</LinearLayout>

完全是个人做的笔记,不喜勿喷
这个demo遇到的问题:1,传值问题2.bitmap显示图片问题,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值