问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

android 怎么用getwallpaper方法

发布网友 发布时间:2022-04-26 18:25

我来回答

2个回答

热心网友 时间:2023-09-21 10:41

  本例主要是先获取壁纸(getWallpaper()),然后对当前壁纸的一些特效处理.大家按步骤一步一步来:


  第一步:新建一个Android工程命名为ImageDemo,工程结构如下:

  第二步:新建一个.java文件,命名为ImageUtil.java,在里面定义一些图片处理方法,代码如下:

  package com.android.tutor;

  import android.graphics.Bitmap;

  import android.graphics.Canvas;

  import android.graphics.LinearGradient;

  import android.graphics.Matrix;

  import android.graphics.Paint;

  import android.graphics.PixelFormat;

  import android.graphics.PorterDuffXfermode;

  import android.graphics.Rect;

  import android.graphics.RectF;

  import android.graphics.Bitmap.Config;

  import android.graphics.PorterDuff.Mode;

  import android.graphics.Shader.TileMode;

  import android.graphics.drawable.Drawable;

  public class ImageUtil {

  //放大缩小图片

  public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){

  int width = bitmap.getWidth();

  int height = bitmap.getHeight();

  Matrix matrix = new Matrix();

  float scaleWidht = ((float)w / width);

  float scaleHeight = ((float)h / height);

  matrix.postScale(scaleWidht, scaleHeight);

  Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

  return newbmp;

  }

  //将Drawable转化为Bitmap

  public static Bitmap drawableToBitmap(Drawable drawable){

  int width = drawable.getIntrinsicWidth();

  int height = drawable.getIntrinsicHeight();

  Bitmap bitmap = Bitmap.createBitmap(width, height,

  drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888

  : Bitmap.Config.RGB_565);

  Canvas canvas = new Canvas(bitmap);

  drawable.setBounds(0,0,width,height);

  drawable.draw(canvas);

  return bitmap;

  

  }

  

  //获得圆角图片的方法

  public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){

  Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap

  .getHeight(), Config.ARGB_8888);

  Canvas canvas = new Canvas(output);

  

  final int color = 0xff424242;

  final Paint paint = new Paint();

  final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

  final RectF rectF = new RectF(rect);

  

  paint.setAntiAlias(true);

  canvas.drawARGB(0, 0, 0, 0);

  paint.setColor(color);

  canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

  

  paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

  canvas.drawBitmap(bitmap, rect, rect, paint);

  

  return output;

  }

  //获得带倒影的图片方法

  public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){

  final int reflectionGap = 4;

  int width = bitmap.getWidth();

  int height = bitmap.getHeight();

  Matrix matrix = new Matrix();

  matrix.preScale(1, -1);

  Bitmap reflectionImage = Bitmap.createBitmap(bitmap,

  0, height/2, width, height/2, matrix, false);

  Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888);

  Canvas canvas = new Canvas(bitmapWithReflection);

  canvas.drawBitmap(bitmap, 0, 0, null);

  Paint deafalutPaint = new Paint();

  canvas.drawRect(0, height,width,height + reflectionGap,

  deafalutPaint);

  canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

  Paint paint = new Paint();

  LinearGradient shader = new LinearGradient(0,

  bitmap.getHeight(), 0, bitmapWithReflection.getHeight()

  + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);

  paint.setShader(shader);

  // Set the Transfer mode to be porter ff and destination in

  paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

  // Draw a rectangle using the paint with our linear gradient

  canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()

  + reflectionGap, paint);

  

  return bitmapWithReflection;

  }

  }

  第三步:修改main.xml布局文件,主要放了两个ImageView控件

  第四步:修改主核心程序,ImageDemo.java,

  package com.android.tutor;

  import android.app.Activity;

  import android.graphics.Bitmap;

  import android.graphics.drawable.Drawable;

  import android.os.Bundle;

  import android.widget.ImageView;

  public class Imagedemo extends Activity {

  private ImageView mImageView01,mImageView02;

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  setupViews();

  }

  

  private void setupViews(){

  mImageView01 = (ImageView)findViewById(R.id.image01);

  mImageView02 = (ImageView)findViewById(R.id.image02);

  

  //获取壁纸返回值是Drawable

  Drawable drawable = getWallpaper();

  //将Drawable转化为Bitmap

  Bitmap bitmap = ImageUtil.drawableToBitmap(drawable);

  //缩放图片

  Bitmap zoomBitmap = ImageUtil.zoomBitmap(bitmap, 100, 100);

  //获取圆角图片

  Bitmap roundBitmap = ImageUtil.getRoundedCornerBitmap(zoomBitmap, 10.0f);

  //获取倒影图片

  Bitmap reflectBitmap = ImageUtil.createReflectionImageWithOrigin(zoomBitmap);

  //这里可以让Bitmap再转化为Drawable

  //    Drawable roundDrawable = new BitmapDrawable(roundBitmap);

  //    Drawable reflectDrawable = new BitmapDrawable(reflectBitmap);

  //    mImageView01.setBackgroundDrawable(roundDrawable);

  //    mImageView02.setBackgroundDrawable(reflectDrawable);

  

  mImageView01.setImageBitmap(roundBitmap);

  mImageView02.setImageBitmap(reflectBitmap);

  }

  

  

热心网友 时间:2023-09-21 10:42

  第一步:新建一个Android工程命名为ImageDemo,工程结构如下:

  第二步:新建一个.java文件,命名为ImageUtil.java,在里面定义一些图片处理方法,代码如下:
  package com.android.tutor;
  import android.graphics.Bitmap;
  import android.graphics.Canvas;
  import android.graphics.LinearGradient;
  import android.graphics.Matrix;
  import android.graphics.Paint;
  import android.graphics.PixelFormat;
  import android.graphics.PorterDuffXfermode;
  import android.graphics.Rect;
  import android.graphics.RectF;
  import android.graphics.Bitmap.Config;
  import android.graphics.PorterDuff.Mode;
  import android.graphics.Shader.TileMode;
  import android.graphics.drawable.Drawable;
  public class ImageUtil {

  //放大缩小图片
  public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){
  int width = bitmap.getWidth();
  int height = bitmap.getHeight();
  Matrix matrix = new Matrix();
  float scaleWidht = ((float)w / width);
  float scaleHeight = ((float)h / height);
  matrix.postScale(scaleWidht, scaleHeight);
  Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
  return newbmp;
  }
  //将Drawable转化为Bitmap
  public static Bitmap drawableToBitmap(Drawable drawable){
  int width = drawable.getIntrinsicWidth();
  int height = drawable.getIntrinsicHeight();
  Bitmap bitmap = Bitmap.createBitmap(width, height,
  drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
  : Bitmap.Config.RGB_565);
  Canvas canvas = new Canvas(bitmap);
  drawable.setBounds(0,0,width,height);
  drawable.draw(canvas);
  return bitmap;
  
  }
  
  //获得圆角图片的方法
  public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){

  Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
  .getHeight(), Config.ARGB_8888);
  Canvas canvas = new Canvas(output);
  
  final int color = 0xff424242;
  final Paint paint = new Paint();
  final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
  final RectF rectF = new RectF(rect);
  
  paint.setAntiAlias(true);
  canvas.drawARGB(0, 0, 0, 0);
  paint.setColor(color);
  canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
  
  paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  canvas.drawBitmap(bitmap, rect, rect, paint);
  
  return output;
  }
  //获得带倒影的图片方法
  public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){
  final int reflectionGap = 4;
  int width = bitmap.getWidth();
  int height = bitmap.getHeight();

  Matrix matrix = new Matrix();
  matrix.preScale(1, -1);

  Bitmap reflectionImage = Bitmap.createBitmap(bitmap,
  0, height/2, width, height/2, matrix, false);

  Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888);

  Canvas canvas = new Canvas(bitmapWithReflection);
  canvas.drawBitmap(bitmap, 0, 0, null);
  Paint deafalutPaint = new Paint();
  canvas.drawRect(0, height,width,height + reflectionGap,
  deafalutPaint);

  canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

  Paint paint = new Paint();
  LinearGradient shader = new LinearGradient(0,
  bitmap.getHeight(), 0, bitmapWithReflection.getHeight()
  + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
  paint.setShader(shader);
  // Set the Transfer mode to be porter ff and destination in
  paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
  // Draw a rectangle using the paint with our linear gradient
  canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
  + reflectionGap, paint);
  
  return bitmapWithReflection;
  }

  }
  第三步:修改main.xml布局文件,主要放了两个ImageView控件
  第四步:修改主核心程序,ImageDemo.java,
  package com.android.tutor;
  import android.app.Activity;
  import android.graphics.Bitmap;
  import android.graphics.drawable.Drawable;
  import android.os.Bundle;
  import android.widget.ImageView;
  public class Imagedemo extends Activity {
  private ImageView mImageView01,mImageView02;

  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  setupViews();
  }
  
  private void setupViews(){
  mImageView01 = (ImageView)findViewById(R.id.image01);
  mImageView02 = (ImageView)findViewById(R.id.image02);
  
  //获取壁纸返回值是Drawable
  Drawable drawable = getWallpaper();
  //将Drawable转化为Bitmap
  Bitmap bitmap = ImageUtil.drawableToBitmap(drawable);
  //缩放图片
  Bitmap zoomBitmap = ImageUtil.zoomBitmap(bitmap, 100, 100);
  //获取圆角图片
  Bitmap roundBitmap = ImageUtil.getRoundedCornerBitmap(zoomBitmap, 10.0f);
  //获取倒影图片
  Bitmap reflectBitmap = ImageUtil.createReflectionImageWithOrigin(zoomBitmap);
  //这里可以让Bitmap再转化为Drawable
  // Drawable roundDrawable = new BitmapDrawable(roundBitmap);
  // Drawable reflectDrawable = new BitmapDrawable(reflectBitmap);
  // mImageView01.setBackgroundDrawable(roundDrawable);
  // mImageView02.setBackgroundDrawable(reflectDrawable);
  
  mImageView01.setImageBitmap(roundBitmap);
  mImageView02.setImageBitmap(reflectBitmap);
  }
  
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
308S标注使用0W-30的机油,如果用5w-40的有影响么 如果飞船在飞行时遇到一粒沙,飞船是否会直接报废?为什么呢?_百度... 火车或地铁行驶中的苍蝇或蚊子这些飞虫是怎么飞的 我一直很疑惑,在地铁公交上的飞虫蚊子之类的,它们不停在杆上,就这么... 固态硬盘的读取是不是都比写入要快啊?才买了个东芝的Q Pro,发现写入496... 我是非护理专业的专科生 但我想考护理资格证 该怎么办呢 我是非护理专业的专科生 但我想考护理资格证 该怎么办? 中专非护理专业单招可以考护理吗 苹果宣布的AppleWatchSeries6和AppleWatchSE有哪些新功能和价格? 描写榕树生命力顽强的诗句 华为手机可以卸载有个com.android.wallpaperdackup是什么 这是什么?有什么用? wallpaper出现图中情况如何处理 com.android.wallpaper是什么进程,删了的后果 安装动态壁纸后设置动态壁纸是出现进程:com.android.wallpaper意外停止该怎么解决? 葛根如何吃法,生吃行吗? 如何食用葛根 节前常见的投资品种有哪些? 葛根怎么吃好 怎样用期货账户买货币基金 葛根吃法 如何? 葛根怎么吃? 以下哪个理财工具风险最大:A货币基金 B 期货 C国债 D国债逆回购? 生吃葛根有什么功效? 葛根生吃什么味道? 新鲜葛根怎么食用 什么叫:货币基金? 什么叫:期货指数? 大国工匠那8位都是谁, 主要事迹 2018大国工匠年度人物事迹及颁奖辞 工人个人事迹怎么写? 想加盟一家女装店,有没有好的品牌推荐呢? android 怎么获取手机桌面上的动态壁纸的 开女装店加盟哪个品牌好? com.android.wallpaper.holosp.是手机病毒吗 android里面的动态壁纸讲解。(wallpaperService)尽量详细,有加分。要中文的,英文不给分。 女装店加盟品牌有哪些 我想开个女装店,不知道怎么找女装的加盟品牌 comandroidwallpaperholospiral甚么意思 加盟一家品牌女装实体店需要多少钱 想加盟女装店,网上找了好几家,百度一看,都是有人说被骗过,不知道 有什么好的女装店?是知名品牌,能免费加盟的 去哪里找服装加盟店? 在网上找的女装加盟有没有保障,会不会受骗?哪个加盟网站比较有保障? 苹果iphone11手机价格是多少 眼疲劳睁不开 总犯困怎么办 上班时总是很想睡觉眼睛睁不开怎么办? 眼晴疲劳,闭着没事,整天只想睡觉,请问这是什么原因,就是眼睛睁不开,想睡 为什么最近总是觉得很困,眼睛有睁不开的感觉,很容易犯困?拜托各位大神 眼睛总感觉睁不开 想睡觉 是咋回事呢 眼睛干涩,老是睁不开眼,犯困