视频直播系统源码,图片叠加-阿里云开发者社区

开发者社区> 开发与运维> 正文

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

简介: 视频直播系统源码,图片叠加
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};
    }
}

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章