视频直播系统源码,图片叠加

2020-10-27 17:54:43 浏览数 (1)

代码语言:javascript复制

package com.zmlxj.customcompass;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
public class Main2Activity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
ImageView image=findViewById(R.id.iv_image);
      /*  // 防止出现Immutable bitmap passed to Canvas constructor错误
        Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),
                R.drawable.compass_bottom).copy(Bitmap.Config.ARGB_8888, true);
        Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable(
                R.drawable.compass_char)).getBitmap();
        Bitmap newBitmap = null;
        newBitmap = Bitmap.createBitmap(bitmap1);
        Canvas canvas = new Canvas(newBitmap);
        Paint paint = new Paint();
        int w = bitmap1.getWidth();
        int h = bitmap1.getHeight();
        int w_2 = bitmap2.getWidth();
        int h_2 = bitmap2.getHeight();
        paint.setColor(Color.GRAY);
        paint.setAlpha(125);
        canvas.drawRect(0, 0, bitmap1.getWidth(), bitmap1.getHeight(), paint);
        paint = new Paint();
        canvas.drawBitmap(bitmap2, Math.abs(w - w_2) / 2,
                Math.abs(h - h_2) / 2, paint);
        canvas.save();
        // 存储新合成的图片
        canvas.restore();
        image.setImageBitmap(newBitmap);*/
        Bitmap bitmap1 = ((BitmapDrawable) getResources().getDrawable(
                R.drawable.compass_bottom)).getBitmap();
        Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable(
                R.drawable.compass_char)).getBitmap();
        Bitmap bitmap3 = ((BitmapDrawable) getResources().getDrawable(
                R.drawable.compass_pointer)).getBitmap();
        Drawable[] array = new Drawable[3];
        array[0] = new BitmapDrawable(bitmap1);
        array[1] = new BitmapDrawable(bitmap2);
        array[2] = new BitmapDrawable(bitmap3);
        LayerDrawable la = new LayerDrawable(array);
        // 其中第一个参数为层的索引号,后面的四个参数分别为left、top、right和bottom
        la.setLayerInset(0, 0, 0, 0, 0);
        la.setLayerInset(1, 20, 20, 20, 20);
      int[] all=  getImageWidthHeight();
        Log.i("获取的值",all[0] "_" all[1]);
        la.setLayerInset(2, 40,  (all[1]/8), 40, 40);
        image.setImageDrawable(la);
    }
    public  int[] getImageWidthHeight(){
        BitmapFactory.Options options = new BitmapFactory.Options();
        /**
         * 最关键在此,把options.inJustDecodeBounds = true;
         * 这里再decodeFile(),返回的bitmap为空,但此时调用options.outHeight时,已经包含了图片的高了
         */
        options.inJustDecodeBounds = true;
//        Bitmap bitmap = BitmapFactory.decodeFile(this.getResources()., options); // 此时返回的bitmap为null
        BitmapFactory.decodeResource(getResources(),R.drawable.compass_bottom,options);
        /**
         *options.outHeight为原始图片的高
         */
        return new int[]{options.outWidth,options.outHeight};
    }
}

0 人点赞