5.7

所花时间(包括上课):1.5

打码量(行):200

博客量(篇):1

了解到知识点:学习绘制集合图片

 

package com.example.myapp;

 

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.PorterDuffXfermode;

import android.graphics.PorterDuff;

import android.util.AttributeSet;

import android.view.View;

 

import androidx.annotation.Nullable;

 

public class ImageCollectionView extends View {

 

    private Paint paint;

    private Bitmap bitmap1;

    private Bitmap bitmap2;

 

    public ImageCollectionView(Context context) {

        super(context);

        init();

    }

 

    public ImageCollectionView(Context context, @Nullable AttributeSet attrs) {

        super(context, attrs);

        init();

    }

 

    public ImageCollectionView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

        init();

    }

 

    private void init() {

        paint = new Paint();

        bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.image1);

        bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.image2);

    }

 

    @Override

    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

 

        // 绘制第一张图片

        canvas.drawBitmap(bitmap1, 50, 50, paint);

 

        // 设置混合模式

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));

 

        // 绘制第二张图片

        canvas.drawBitmap(bitmap2, 150, 150, paint);

 

        // 清除混合模式

        paint.setXfermode(null);

    }

}

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

 

    <com.example.myapp.ImageCollectionView

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

 

</RelativeLayout>

posted @ 2024-05-07 17:13  赵千万  阅读(4)  评论(0编辑  收藏  举报