當前位置:首頁 » 安卓系統 » androidbitmap背景

androidbitmap背景

發布時間: 2022-06-25 04:17:31

⑴ android中paint如何設置背景圖片

1.創建一個背景圖大小的Bitmap Bitmap bitmap=Bitmap.createBitmap(X, Y, Config.ARGB_8888);

2.Canvas canvas=new Canvas(bitmap); //創建畫布Paint paint=new Paint(); //畫筆

3。在canvas上畫東西取得背景圖片的Bitmap canvas.drawBitmap(backgroundBitmap, Rect , Rect , paint);
按照上面的步驟就可以設置背景圖片了

⑵ android createBitmap,如何設置背景色

此方法是根據int[] colors色素組創建Bitmap,你可以到這里看一下:http://blog.csdn.net/gaomatrix/article/details/6532468

⑶ Android開發,動態設置Activity的layout背景圖片問題

Bitmap img = BitmapFactory.decodeFile(BackgroundPath);

執行這行代碼的時候,拋出的異常,估計是報內存溢出
原因很簡單,Bitmap 是用位元組來代碼像素點的
如果你的圖片非常大,那麼android需要創建一個非常大的數組來生成bitmap對象
這時候就會拋出異常
建議你創建Bitmap前,先測試一下圖片的大小,把圖片的大小壓縮成屏蔽的大小
再用來做layout的Background

⑷ 如何實現Android 布局背景模糊化處理

在模仿 IOS 密碼輸入頁面的時候發現其背景有模糊處理,於是了解了一下並記錄下來,以便使用.在Android 中具體實現方法如下

查考 http://www.cnblogs.com/lipeil/p/3997992.html

java代碼
private void applyBlur() {

// 獲取壁紙管理器
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this.getContext());
// 獲取當前壁紙
Drawable wallpaperDrawable = wallpaperManager.getDrawable();
// 將Drawable,轉成Bitmap
Bitmap bmp = ((BitmapDrawable) wallpaperDrawable).getBitmap();

blur(bmp);
}

下面之所以要進行small 和big的處理,是因為僅僅靠ScriptIntrinsicBlur
來處理模式,不能到達更模式的效果,如果需要加深模式效果就需要先把背景圖片縮小,在處理完之後再放大.這個可以使用Matrix
來實現,而且這樣可以縮短模糊化得時間

Java代碼
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void blur(Bitmap bkg) {
long startMs = System.currentTimeMillis();
float radius = 20;

bkg = small(bkg);
Bitmap bitmap = bkg.(bkg.getConfig(), true);

final RenderScript rs = RenderScript.create(this.getContext());
final Allocation input = Allocation.createFromBitmap(rs, bkg, Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
final Allocation output = Allocation.createTyped(rs, input.getType());
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setRadius(radius);
script.setInput(input);
script.forEach(output);
output.To(bitmap);

bitmap = big(bitmap);
setBackground(new BitmapDrawable(getResources(), bitmap));
rs.destroy();
Log.d("zhangle","blur take away:" + (System.currentTimeMillis() - startMs )+ "ms");
}

private static Bitmap big(Bitmap bitmap) {
Matrix matrix = new Matrix();
matrix.postScale(4f,4f); //長和寬放大縮小的比例
Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
return resizeBmp;
}

private static Bitmap small(Bitmap bitmap) {
Matrix matrix = new Matrix();
matrix.postScale(0.25f,0.25f); //長和寬放大縮小的比例
Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
return resizeBmp;
}

⑸ android 在Java代碼中,我調用一個函數後返回Bitmap圖,想將它設為背景,每次調用setbackground都出錯

一般情況下,錯誤信息中都會含帶錯誤出現的頁面位置,比如多少行,那麼就到你部署的程序的伺服器中查看work下面編譯成java代碼以後的jsp頁面。用記事本或者notepad++打開,定位到那一行,就可以確定是哪一句的問題。
可以在jsp頁面中的伺服器腳本中寫System.out.println("123");看是否輸出(此處的System.out.println("123")盡量多寫幾行,在你認為有可能發生錯誤的一段內也可以),逐漸縮小范圍。
最後一種就是:空指針異常都是由於「對象點」引起的,這種寫法的地方都值得懷疑一下,尤其是傳值過來的。

⑹ 怎麼實現Android 布局背景模糊化處理

在模仿 IOS 密碼輸入頁面的時候發現其背景有模糊處理,於是了解了一下並記錄下來,以便使用.在Android 中具體實現方法如下
private void applyBlur() {

// 獲取壁紙管理器
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this.getContext());
// 獲取當前壁紙
Drawable wallpaperDrawable = wallpaperManager.getDrawable();
// 將Drawable,轉成Bitmap
Bitmap bmp = ((BitmapDrawable) wallpaperDrawable).getBitmap();

blur(bmp);
}

下面之所以要進行small 和big的處理,是因為僅僅靠ScriptIntrinsicBlur 來處理模式,不能到達更模式的效果,如果需要加深模式效果就需要先把背景圖片縮小,在處理完之後再放大.這個可以使用Matrix 來實現,而且這樣可以縮短模糊化得時間
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void blur(Bitmap bkg) {
long startMs = System.currentTimeMillis();
float radius = 20;

bkg = small(bkg);
Bitmap bitmap = bkg.(bkg.getConfig(), true);

final RenderScript rs = RenderScript.create(this.getContext());
final Allocation input = Allocation.createFromBitmap(rs, bkg, Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
final Allocation output = Allocation.createTyped(rs, input.getType());
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setRadius(radius);
script.setInput(input);
script.forEach(output);
output.To(bitmap);

bitmap = big(bitmap);
setBackground(new BitmapDrawable(getResources(), bitmap));
rs.destroy();
Log.d("zhangle","blur take away:" + (System.currentTimeMillis() - startMs )+ "ms");
}

private static Bitmap big(Bitmap bitmap) {
Matrix matrix = new Matrix();
matrix.postScale(4f,4f); //長和寬放大縮小的比例
Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
return resizeBmp;
}

private static Bitmap small(Bitmap bitmap) {
Matrix matrix = new Matrix();
matrix.postScale(0.25f,0.25f); //長和寬放大縮小的比例
Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
return resizeBmp;
}

⑺ Android 怎麼用網上的圖片設置ImageView背景

Android 用網上的圖片設置ImageView背景可以使用如下方法:


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myimage);
ImageView image1 = (ImageView) findViewById(R.myImage.image);
//Bitmap bitmap = getLoacalBitmap(「/aa/aa.jpg」); //從本地取圖片
Bitmap bitmap = getHttpBitmap(「http://blog.3gstdy.com/wp-content/themes/twentyten/images/headers/path.jpg」); //從網上取圖片
image1 .setImageBitmap(bitmap); //設置Bitmap
}
/**
* 載入本地圖片
* http://bbs.3gstdy.com
* @param url
* @return
*/
public static Bitmap getLoacalBitmap(String url) {
try {
FileInputStream fis = new FileInputStream(url);
return BitmapFactory.decodeStream(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
/**
* 從伺服器取圖片
*http://bbs.3gstdy.com
* @param url
* @return
*/
public static Bitmap getHttpBitmap(String url) {
URL myFileUrl = null;
Bitmap bitmap = null;
try {
Log.d(TAG, url);
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setConnectTimeout(0);
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}

⑻ 如何將android中兩種bitmap疊加居中形成一個比較漂亮的圖

常用場景:給桌面圖標添加底盤背景圖 public Bitmap addbackground4onlyicon(Resources r, Bitmap icon) { Bitmap b1 = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap1);; Bitmap b2 = icon; if (!b1.isMutable()) { //設置圖片為背景為透明 b1 = b1.(Bitmap.Config.ARGB_8888, true); } Paint paint = new Paint(); Canvas canvas = new Canvas(b1); int b1w = b1.getWidth(); int b1h = b1.getHeight(); int b2w = b2.getWidth(); int b2h = b2.getHeight(); int bx = (b1w - b2w) / 2; int by = (b1h - b2h) / 2; canvas.drawBitmap(b2, bx, by, paint);//疊加新圖b2 並且居中 canvas.save(Canvas.ALL_SAVE_FLAG); canvas.restore(); return b1; }

⑼ android bitmap使用時注意什麼

一、 問題的背景和意義
在Android移動應用開發中,對Bitmap的不小心處理,很容易引起程序內存空間耗盡而導致的程序崩潰問題。比如我們常遇到的問題:
java.lang.OutofMemoryError: bitmap size exceeds VM budget.
導致該問題的出現,一般由以下幾方面原因導致:
引動設備一般存儲空間非常有限。當然不同設備分配給應用的內存空間是不同的。但相對不但提高的設備解析度而言,內存的分配仍然是相對緊張的。
Bitmap對象常常佔用大量的內存空間,比如:對於2592*1936的設備,如果採用ARGB_8888的格式載入圖像,內存佔用將達到19MB空間。
在Anroid App中經常用到ListView,ViewPager等控制項,這些控制項常會包含較大數量的圖片資源。
二、 問題及場景分析
1 高效地載入大圖片。
BitmapFactory類提供了一些載入圖片的方法:decodeByteArray(), decodeFile(), decodeResource(), 等等。
為了避免佔用較大內存,經常使用BitmapFactory.Options 類,設置inJustDecodeBounds屬性為true。
//
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds =true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
為了避免java.lang.OutOfMemory 的異常,我們在真正decode圖片之前檢查它的尺寸,除非你確定這個數據源提供了准確無誤的圖片且不會導致佔用過多的內存。
載入一個按比例縮小的版本到內存中。例如,如果把一個原圖是1024*768 pixel的圖片顯示到ImageView為128*96 pixel的縮略圖就沒有必要把整張圖片都載入到內存中。為了告訴解碼器去載入一個較小的圖片到內存,需要在你的BitmapFactory.Options 中設置 inSampleSize 為true 。例如, 一個解析度為2048x1536 的圖片,如果設置inSampleSize 為4,那麼會產出一個大概為512x384的圖片。載入這張小的圖片僅僅使用大概0.75MB,如果是載入全圖那麼大概要花費12MB(假設bitmap的配置是ARGB_8888).
2 不要在主線程處理圖片。

眾所周知的問題,不再贅述。
注意兩點:1. 為了保證使用的資源能被回收,建議使用WeakReference, 以應用內存內存緊張時,回收部分資源,保證程序進程不被殺死。
2. 避免非同步任務的長時間耗時操作,在任務執行結束後,及時釋放資源。
3 管理Bitmap內存。
在Android開發中,載入一個圖片到界面很容易,但如果一次載入大量圖片就復雜多了。在很多情況下(比如:ListView,GridView或ViewPager),能夠滾動的組件需要載入的圖片幾乎是無限多的。
有些組件的child view在不顯示時會回收,並循環使用,如果沒有任何對bitmap的持久引用的話,垃圾回收器會釋放你載入的bitmap。這沒什麼問題,但當這些圖片再次顯示的時候,要想避免重復處理這些圖片,從而達到載入流暢的效果,就要使用內存緩存和本地緩存了,這些緩存可以讓你快速載入處理過的圖片。
3.1 內存緩存
內存緩存以犧牲內存的代價,帶來快速的圖片訪問。LruCache類(API Level 4之前可以使用Support Library)非常適合圖片緩存任務,在一個LinkedHashMap中保存著對Bitmap的強引用,當緩存數量超過容器容量時,刪除最近最少使用的成員(LRU)。
注意:在過去,非常流行用SoftReference或WeakReference來實現圖片的內存緩存,但現在不再推薦使用這個方法了。因為從Android 2.3 (API Level 9)之後,垃圾回收器會更積極的回收soft/weak的引用,這將導致使用soft/weak引用的緩存幾乎沒有緩存效果。順帶一提,在Android3.0(API Level 11)以前,bitmap是儲存在native 內存中的,所以系統以不可預見的方式來釋放bitmap,這可能會導致短時間超過內存限制從而造成崩潰。

⑽ gdi+的BITMAP或者IMAGE怎麼設置背景色不要透明

在布局文件中,可以使用屬性android:background="#00ffffff",在方法中使用view.setBackgroundColor(android.graphics.Color.parseColor("#00ffffff")); 在GrilView中注意,如果有子視圖,子視圖的背景設為透明才能使GrilView背景全部透明。

熱點內容
怎麼查看我的wifi密碼 發布:2024-04-25 18:54:43 瀏覽:757
fckeditorforjava 發布:2024-04-25 18:50:27 瀏覽:624
優酷上傳視頻需要多久 發布:2024-04-25 18:33:05 瀏覽:675
inf12編譯器 發布:2024-04-25 18:15:39 瀏覽:99
撲克總督3安卓哪裡下載 發布:2024-04-25 18:10:02 瀏覽:395
什麼網站是php 發布:2024-04-25 18:03:42 瀏覽:221
java教程免費下載 發布:2024-04-25 18:02:01 瀏覽:443
i西安編程 發布:2024-04-25 16:55:35 瀏覽:263
核磁看壓縮 發布:2024-04-25 16:37:22 瀏覽:432
訪問不上光貓 發布:2024-04-25 16:13:44 瀏覽:319