android 圖片疊加xml,Android實現圖片疊加效果的兩種方法

本文實例講述了Android實現圖片疊加效果的兩種方法。,具體如下:

效果圖:

第一種:

2025093948-0.png

第二種:

2025092954-1.png

第一種是通過canvas畫出來的效果:

public void first(View v) {

// 防止出現Immutable bitmap passed to Canvas constructor錯誤

Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),

R.drawable.apple).copy(Bitmap.Config.ARGB_8888, true);

Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable(

R.drawable.go)).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.ALL_SAVE_FLAG);

// 存儲新合成的圖片

canvas.restore();

image.setImageBitmap(newBitmap);

}

Canvas canvas = new Canvas(newBitmap); 當以newBitmap創建Canvas時,所以操作都已經在該圖上實現了。

該例子可以想做是播放器開始播放的效果,計算好中間位置,先覆蓋一層透明灰色的正方形,然后在中間畫上自己播放的按鈕。

第二種是使用系統的LayerDrawable類,該類主要用來控制多個圖片的結合:

public void second(View v) {

Bitmap bitmap1 = ((BitmapDrawable) getResources().getDrawable(

R.drawable.apple)).getBitmap();

Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable(

R.drawable.go)).getBitmap();

Drawable[] array = new Drawable[2];

array[0] = new BitmapDrawable(bitmap1);

array[1] = new BitmapDrawable(bitmap2);

LayerDrawable la = new LayerDrawable(array);

// 其中第一個參數為層的索引號,后面的四個參數分別為left、top、right和bottom

la.setLayerInset(0, 0, 0, 0, 0);

la.setLayerInset(1, 20, 20, 20, 20);

image.setImageDrawable(la);

}

關聯數組array,控制每一層的位置

注意:上面防止出現Immutable bitmap passed to Canvas constructor錯誤

原因是如果不用copy的方法,直接引用會對資源文件進行修改,而Android是不允許在代碼里修改res文件里的圖片

layer-list可以將多個圖片按照順序層疊起來。

語法:在drawalbe/drawable-layer.xml中

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541677.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541677.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541677.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Win10系列:VC++ 定時器

計時器機制俗稱"心跳",表示以特定的頻率持續觸發特定事件和執行特定程序的機制。在開發Windows應用商店應用的過程中,可以使用定義在Windows::UI::Xaml命名空間中的DispatcherTimer類來創建計時器。DispatcherTimer類包含了如下的成員&#xf…

dbms系統 rdbms_DBMS與傳統文件系統之間的區別

dbms系統 rdbmsIntroduction 介紹 DBMS and Traditional file system have some advantages, disadvantages, applications, functions, features, components and uses. So, in this article, we will discuss these differences, advantages, disadvantages and many other …

android 百度地圖api密鑰,Android百度地圖開發獲取秘鑰之SHA1

最近在做一個關于百度地圖的開發。不過在正式開發之前還必須要在百度地圖API官網里先申請秘鑰,而在申請秘鑰的過程中,就需要獲取一個所謂的SHA1值。如上所示,但是由于不是正式開發,所以以上的發布版和開發版的SHA1可以先填寫相同。…

單位矩陣的逆| 使用Python的線性代數

Prerequisites: 先決條件: Defining a Matrix 定義矩陣 Identity Matrix 身份矩陣 There are matrices whose inverse is the same as the matrices and one of those matrices is the identity matrix. 有些矩陣的逆與矩陣相同,并且這些矩陣之一是單位…

華為榮耀七能升級鴻蒙系統嗎,華為鴻蒙系統來了,你知道哪些華為手機榮耀手機可以升級嗎?...

從鴻蒙系統第一次開始登場,到現在慢慢有許多鴻蒙系統設備出現,手機市場的格局似乎又要升級變化了。科技樹兒了解到,在某數碼博主經過和相關人員的溝通核實之后,目前暫定的是搭載華為麒麟710芯片以上的機型,無論華為或榮…

day5-shutil模塊

一、簡述 我們在日常處理文件時,經常用到os模塊,但是有的時候你會發現,像拷貝、刪除、打包、壓縮等文件操作,在os模塊中沒有對應的函數去操作,下面我們就來講講高級的 文件、文件夾、壓縮包 處理模塊:shuti…

matlab中now函數_now()方法以及JavaScript中的示例

matlab中now函數JavaScript now()方法 (JavaScript now() method) now() method is a Date class method, it is used to current time in milliseconds, it returns the total number of milliseconds since 01st January 1970, 00:00:00 UTC. now()方法是Date類的一種方法&am…

android 集成x5內核時 本地沒有,騰訊瀏覽服務-接入文檔

三、SDK集成步驟1. 第一步下載 SDK jar 包放到工程的libs目錄下,將源碼和XML里的系統包和類替換為SDK里的包和類,具體對應如下:系統內核SDK內核android.webkit.ConsoleMessagecom.tencent.smtt.export.external.interfaces.ConsoleMessageand…

java vector_Java Vector sureCapacity()方法與示例

java vector向量類別sureCapacity()方法 (Vector Class ensureCapacity() method) ensureCapacity() method is available in java.util package. sureCapacity()方法在java.util包中可用。 ensureCapacity() method is used to ensure the capacity of this Vector when requi…

Tcl與Design Compiler (十二)——綜合后處理

本文如果有錯,歡迎留言更正;此外,轉載請標明出處 http://www.cnblogs.com/IClearner/ ,作者:IC_learner 概述 前面也講了一些綜合后的需要進行的一些工作,這里就集中講一下DC完成綜合了,產生了…

Java短類的compareTo()方法和示例

簡短的類compareTo()方法 (Short class compareTo() method) compareTo() method is available in java.lang package. compareTo()方法在java.lang包中可用。 compareTo() method is used to check equality or inequality for this Short object against the given Short obj…

四則運算網頁版

一.設計思想: 1)寫出一個菜單界面,有兩個選項一個是分數,一個是整數。 2)而這兩個標簽后面則是轉向其更詳細的菜單,題目數量,有無括號,運算的項數等等詳細功能,再點擊這兩…

Java RandomAccessFile seek()方法與示例

RandomAccessFile類seek()方法 (RandomAccessFile Class seek() method) seek() method is available in java.io package. seek()方法在java.io包中可用。 seek() method is used to sets the file pointer position calculated from the starting of this file at which the …

Javascript開發技巧(JS中的變量、運算符、分支結構、循環結構)

一、Js簡介和入門 繼續跟進JS開發的相關教程。 <!-- [使用JS的三種方式] 1、HTML標簽中內嵌JS&#xff08;不提倡使用&#xff09;&#xff1a; 示例&#xff1a;<button οnclick"javascript:alert(你真點啊&#xff01;)">有本事點我呀&#xff01;&#…

android 顏色范圍,Android系統顏色的適用范圍

###All Clickable Views:ripple effect (Lollipop only) — “colorControlHighlight”###Status Bar:background (Lollipop only) – “colorPrimaryDark”###Navigation Bar:background (Lollipop only) – “android:navigationBarColor”###EditText:underline (unfocused)…

bytevalue_Java Short類byteValue()方法及示例

bytevalue短類byteValue()方法 (Short class byteValue() method) byteValue() method is available in java.lang package. byteValue()方法在java.lang包中可用。 byteValue() method is used to return the value denoted by this Short object converted to type byte (by …

分布式交換機配置備份和還原

1.備份和還原vSphere Distributed Switch配置 1.1導出 vSphere Distributed Switch 配置 可以將 vSphere Distributed Switch 和分布式端口組配置導出到某一文件。該文件保留有效的網絡配置&#xff0c;使這些配置能夠傳輸至其他環境。 步驟&#xff1a; 1) 在 vSphere Web Cli…

html自動執行函數,JS 自執行函數原理及用法

js自執行函數&#xff0c;聽到這個名字&#xff0c;首先會聯想到函數。接下來&#xff0c;我來定義一個函數&#xff1a;function aaa(a,b){return sum a b}定義了一個名為aaa的函數&#xff0c;在里面可以計算兩個數的和。如果想執行它&#xff0c;就必須得調用它&#xff0…

java reverse_Java Integer類reverse()方法與示例

java reverse整數類reverse()方法 (Integer class reverse() method) reverse() method is available in java.lang package. reverse()方法在java.lang包中可用。 reverse() method is used to returns the value generated by reversing the order of bits in binary 2s comp…

華為鴻蒙系統好在哪,華為鴻蒙2.0可以替代安卓嗎,華為鴻蒙2.0優勢在哪

在華為開發者大會上&#xff0c;華為消費業務CEO 余承東&#xff0c;正式發布鴻蒙OS2.0&#xff0c;并宣布華為鴻蒙OS將全面啟用全場景生態&#xff0c;并將于2020年12月發布手機版。余承東還表示&#xff0c;明年&#xff0c;華為的智能手機將全面升級&#xff0c;以支持鴻蒙操…