首页 文章 精选 留言 我的

精选列表

搜索[笔记工具],共10011篇文章
优秀的个人博客,低调大师

任务调度工具Quartz入门笔记

一,导包 1)官网下载:http://www.quartz-scheduler.org/downloads/ 2)Maven <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> 二,实例 1.先定义一个作业任务类,写我们的执行代码 package cn.zyzpp.quartz.demo; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobDetail; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; import org.quartz.Trigger; /** * Created by yster@foxmail.com * 2018年4月9日 下午11:00:52 */ public class HelloJob implements Job { //第(2)种获取传入的参数的方法:setter方法 属性对应key的值 private String setKey; public void execute(JobExecutionContext context) throws JobExecutionException { //获取JobDetail对象 String now = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss").format(new Date()); JobDetail detail = context.getJobDetail(); String group = detail.getKey().getGroup(); String value = detail.getJobDataMap().getString("key"); //获取Trigger对象 Trigger trigger = context.getTrigger(); value= trigger.getJobDataMap().getString("key"); //获取JobDataMap对象 //第(1)种获取传入的参数的方法 JobDataMap data = context.getMergedJobDataMap(); value = data.getString("key");//相同key 调用trigger的value //System.out.println(group + ":" + value + " at " + now + setKey); //获取Jobkey对象 JobKey jobKey = trigger.getJobKey(); String name = jobKey.getName(); group = jobKey.getGroup(); try { Thread.sleep(5000l); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(now + ":" + name + " " +group); } public String getSetKey() { return setKey; } public void setSetKey(String setKey) { this.setKey = setKey; } } 2. package cn.zyzpp.quartz.demo; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.JobBuilder; import org.quartz.SimpleScheduleBuilder; import org.quartz.SimpleTrigger; import org.quartz.TriggerBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.Trigger; import org.quartz.impl.StdSchedulerFactory; /** * Created by yster@foxmail.com 2018年4月9日 下午10:57:14 */ public class QuartzTest { public static void main(String[] args) { SimpleDateFormat sf =new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); try { // Quartz 作业:定义一个JobDetail:为Job类设置属性 JobDetail jobDetail = JobBuilder.newJob(HelloJob.class) // 定义Job类为HelloQuartz类,这是真正的执行逻辑所在 .withIdentity("job1", "group1") // 定义name/group .usingJobData("key", "job-value") // 自定义参数 .usingJobData("setKey", "setValue").build(); // System.out.println(jobDetail.getKey().getName()); // System.out.println(jobDetail.getKey().getGroup()); // System.out.println(jobDetail.getJobClass()); // 定义任务开始时间以及结束时间 Date startDate = new Date(); System.out.println("scheduler开始:" + sf.format(new Date())); startDate.setTime(new Date().getTime() + 2000); Date endDate = new Date(); endDate.setTime(startDate.getTime() + 4000); // Quartz触发器:定义一个Trigger Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("trigger1", "group1") // 定义name/group // .startNow()//一旦加入scheduler,立即生效 .startAt(startDate) .endAt(endDate) .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(1) // 每隔一秒执行一次 .repeatForever()) // 一直执行,奔腾到老不停歇 .usingJobData("key", "trigger-value") .build(); //SimpleTrigger 在一个指定时间段内执行一次作业任务 或者在指定时间间隔内多次执行作业任务 SimpleTrigger simpleTrigger = (SimpleTrigger) TriggerBuilder .newTrigger() .withIdentity("trigger1", "group1") // 定义name/group .startAt(startDate) .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(2) .withRepeatCount(3)) //执行第1次后再执行3次(SimpleTrigger.REPEAT_INDEFINITELY) .build(); //CronTrigger 基于 cron 表达式,更常用 CronTrigger cronTrigger = (CronTrigger) TriggerBuilder .newTrigger() .withIdentity("trigger1", "group1") // 定义name/group .startAt(startDate) .withSchedule( //Cron表达式:[秒][分][时][日][月][周][年] (周日1-周六7,年可不写) *每 ?不关心 -至 #第 /递增 ,和 L最后 W最近工作日 CronScheduleBuilder.cronSchedule("0/2 * * * * ? * ") ) .build(); // 调度类链接“工作”和“触发器”到一起,并执行它 // 创建scheduler StdSchedulerFactory sfact = new StdSchedulerFactory(); Scheduler scheduler = sfact.getScheduler(); // 加入这个调度 返回第一次执行时间 scheduler.scheduleJob(jobDetail, cronTrigger); //System.out.println("调度器:" + sf.format(scheduler.scheduleJob(jobDetail, cronTrigger))); // 启动之 scheduler.start(); // 运行一段时间后暂停 可再次start 当前主线程不终止 Thread.sleep(10000l); // scheduler.standby(); // System.out.println("暂停:三秒后再次开启"); // Thread.sleep(3000l); // scheduler.start(); // Thread.sleep(5000l); scheduler.shutdown(true);//true等待执行的job完成后关闭scheduler,false直接关闭 System.out.println("scheduler终止" + sf.format(new Date())); } catch (Exception e) { e.printStackTrace(); } } } 三,干货 1)Cron表达式 2)/org/quartz/quartz.properties 3)集成Spring <!-- JobDetail --> <!-- (1)simpleJobDetail : 定义任务类和任务方法--> <bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="myBean" /> <property name="targetMethod" value="printMessage" /> </bean> <!--(2)继承 QuartzJobBean 传参--> <bean id="firstComplexJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass" value="com.imooc.springquartz.quartz.FirstScheduledJob" /> <property name="jobDataMap"> <map> <entry key="anotherBean" value-ref="anotherBean" /> </map> </property> <property name="durability" value="true"/> </bean> <!--Trigger--> <!-- (1)距离当前时间1秒之后执行,之后每隔两秒钟执行一次 --> <bean id="mySimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> <property name="jobDetail" ref="simpleJobDetail"/> <property name="startDelay" value="1000"/> <property name="repeatInterval" value="2000"/> </bean> <!-- (2)每隔5秒钟执行一次 --> <bean id="myCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="firstComplexJobDetail"/> <property name="cronExpression" value="0/5 * * ? * *"/> </bean> <!-- (3)Scheduler --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="jobDetails"> <list> <ref bean="simpleJobDetail"/> <ref bean="firstComplexJobDetail"/> </list> </property> <property name="triggers"> <list> <ref bean="mySimpleTrigger"/> <ref bean="myCronTrigger"/> </list> </property> </bean> 1.MyBean.java import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.stereotype.Component; @Component("myBean") public class MyBean { public void printMessage() { // 打印当前的执行时间,格式为2017-01-01 00:00:00 Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("MyBean Executes!" + sf.format(date)); } } 2.AnotherBean.java import org.springframework.stereotype.Component; @Component("anotherBean") public class AnotherBean { public void printAnotherMessage() { System.out.println("AnotherMessage"); } } 3.FirstScheduledJob.java import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class FirstScheduledJob extends QuartzJobBean{ private AnotherBean anotherBean; public void setAnotherBean(AnotherBean anotherBean){ this.anotherBean = anotherBean; } @Override protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("FirstScheduledJob Executes!" + sf.format(date)); this.anotherBean.printAnotherMessage(); } }

优秀的个人博客,低调大师

Android笔记:bitmap转换与处理相关工具类,Bitmap与DrawAble与byte[]与InputStream之间的转换

1.将view转为bitmap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 // 将view转为bitmap public static Bitmap getBitmapFromView(View view) { // Define a bitmap with the same size as the view Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); // Bind a canvas to it Canvas canvas = new Canvas(returnedBitmap); // Get the view's background Drawable bgDrawable = view.getBackground(); if (bgDrawable != null ) // has background drawable, then draw it on the canvas bgDrawable.draw(canvas); else // does not have background drawable, then draw white background on // the canvas canvas.drawColor(Color.WHITE); // draw the view on the canvas view.draw(canvas); // return the bitmap return returnedBitmap; } 2.将view转为bitmap 1 2 3 4 5 6 7 8 // 将view转为bitmap public static Bitmap viewToBitmap(View view) { view.setDrawingCacheEnabled( true ); view.buildDrawingCache(); Bitmap bm = view.getDrawingCache(); return bm; } 3.将xml转为bitmap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 // 将xml转为bitmap public static Bitmap convertBitmapFromXML(Context context, String clusterSize, Bitmap bm) { View layout = LayoutInflater.from(context).inflate(R.layout.estatecartlist_item, null ); View bitmapView = layout.findViewById(R.id.estatecartlist_item_bitmap); TextView xml_text = (TextView) layout.findViewById(R.id.item_estatecart_tv_name); ImageView image = (ImageView) layout.findViewById(R.id.item_estatecart_iv_main); image.setImageBitmap(bm); xml_text.setText(clusterSize); bitmapView.measure(MeasureSpec.makeMeasureSpec( 0 , MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec( 0 , MeasureSpec.UNSPECIFIED)); bitmapView.layout( 0 , 0 , bitmapView.getMeasuredWidth(), bitmapView.getMeasuredHeight()); final Bitmap clusterBitmap = Bitmap.createBitmap(bitmapView.getMeasuredWidth(), bitmapView.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(clusterBitmap); bitmapView.draw(canvas); return clusterBitmap; } ==============参考资料=================== * 1.http://stackoverflow.com/questions/7200535/how-to-convert-views-to-bitmap * 2.http://stackoverflow.com/questions/5536066/convert-view-to-bitmap-on-android/9595919#9595919 * 3.http://stackoverflow.com/questions/12402392/android-converting-xml-view-to-bitmap-without-showing-it 4.图片缩放与压缩 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 // 按大小缩放 private Bitmap getimage(String srcPath) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 开始读入图片,此时把options.inJustDecodeBounds 设回true了 newOpts.inJustDecodeBounds = true ; Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts); // 此时返回bm为空 newOpts.inJustDecodeBounds = false ; int w = newOpts.outWidth; int h = newOpts.outHeight; // 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为 float hh = 800f; // 这里设置高度为800f float ww = 480f; // 这里设置宽度为480f // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1 ; // be=1表示不缩放 if (w > h && w > ww) { // 如果宽度大的话根据宽度固定大小缩放 be = ( int ) (newOpts.outWidth / ww); } else if (w < h && h > hh) { // 如果高度高的话根据宽度固定大小缩放 be = ( int ) (newOpts.outHeight / hh); } if (be <= 0 ) be = 1 ; newOpts.inSampleSize = be; // 设置缩放比例 // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 bitmap = BitmapFactory.decodeFile(srcPath, newOpts); return compressImage(bitmap); // 压缩好比例大小后再进行质量压缩 } // 图片质量压缩 private static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100 , baos); // 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 int options = 100 ; while (baos.toByteArray().length / 1024 > 100 ) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩 baos.reset(); // 重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, options, baos); // 这里压缩options%,把压缩后的数据存放到baos中 options -= 10 ; // 每次都减少10 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); // 把压缩后的数据baos存放到ByteArrayInputStream中 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null , null ); // 把ByteArrayInputStream数据生成图片 return bitmap; } // 图片按比例大小压缩 private static Bitmap comp(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100 , baos); if (baos.toByteArray().length / 1024 > 1024 ) { // 判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出 baos.reset(); // 重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, 50 , baos); // 这里压缩50%,把压缩后的数据存放到baos中 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 开始读入图片,此时把options.inJustDecodeBounds 设回true了 newOpts.inJustDecodeBounds = true ; Bitmap bitmap = BitmapFactory.decodeStream(isBm, null , newOpts); newOpts.inJustDecodeBounds = false ; int w = newOpts.outWidth; int h = newOpts.outHeight; // 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为 float hh = 800f; // 这里设置高度为800f float ww = 480f; // 这里设置宽度为480f // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1 ; // be=1表示不缩放 if (w > h && w > ww) { // 如果宽度大的话根据宽度固定大小缩放 be = ( int ) (newOpts.outWidth / ww); } else if (w < h && h > hh) { // 如果高度高的话根据宽度固定大小缩放 be = ( int ) (newOpts.outHeight / hh); } if (be <= 0 ) be = 1 ; newOpts.inSampleSize = be; // 设置缩放比例 // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 isBm = new ByteArrayInputStream(baos.toByteArray()); bitmap = BitmapFactory.decodeStream(isBm, null , newOpts); return compressImage(bitmap); // 压缩好比例大小后再进行质量压缩 } 5.图片转为文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // 图片转为文件 public static boolean saveBitmap2file(Bitmap bmp, String filename) { CompressFormat format = Bitmap.CompressFormat.JPEG; int quality = 100 ; OutputStream stream = null ; try { stream = new FileOutputStream( "/sdcard/" + filename); } catch (FileNotFoundException e) { e.printStackTrace(); } return bmp.compress(format, quality, stream); } 6.屏幕截屏方法 1 2 3 4 5 6 7 8 9 // 屏幕截屏方法 获取当前屏幕bitmap,转换成bytes[] 调用 UI分享方法 public void printscreen_share(View v) { View view1 = getWindow().getDecorView(); Display display = getWindowManager().getDefaultDisplay(); view1.layout( 0 , 0 , display.getWidth(), display.getHeight()); view1.setDrawingCacheEnabled( true ); Bitmap bitmap = Bitmap.createBitmap(view1.getDrawingCache()); } 7.把Bitmap 转成 Byte 1 2 3 4 5 6 7 // 把Bitmap 转成 Byte public static byte [] Bitmap2Bytes(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100 , baos); return baos.toByteArray(); } 8.图片转为文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 // 图片转为文件 public static boolean saveBitmap2file(Bitmap bmp) { CompressFormat format = Bitmap.CompressFormat.PNG; int quality = 100 ; OutputStream stream = null ; try { // 判断SDcard状态 if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { // 错误提示 return false ; } // 检查SDcard空间 File SDCardRoot = Environment.getExternalStorageDirectory(); if (SDCardRoot.getFreeSpace() < 10000 ) { // 弹出对话框提示用户空间不够 Log.e( "Utils" , "存储空间不够" ); return false ; } // 在SDcard创建文件夹及文件 File bitmapFile = new File(SDCardRoot.getPath() + FILE_PATH); bitmapFile.getParentFile().mkdirs(); // 创建文件夹 stream = new FileOutputStream(SDCardRoot.getPath() + FILE_PATH); // "/sdcard/" } catch (FileNotFoundException e) { e.printStackTrace(); } return bmp.compress(format, quality, stream); } 9.下载图片 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 // 下载图片 public static Bitmap loadImage(String... params) { InputStream is = null ; Bitmap bitmap = null ; try { URL url = new URL(params[ 0 ]); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout( 5000 ); conn.setConnectTimeout( 5000 ); if (HttpURLConnection.HTTP_OK != conn.getResponseCode()) { // 网络连接异常 Log.e( "" , "loadImage连接异常" ); return null ; } is = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(is); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if ( null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return bitmap; } 10.byte[]转换成Bitmap 1 2 3 4 5 6 7 8 9 // byte[]转换成Bitmap public static Bitmap Bytes2Bitmap( byte [] b) { if (b.length != 0 ) { return BitmapFactory.decodeByteArray(b, 0 , b.length); } return null ; } 11.将字符串转换成Bitmap类型 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public static Bitmap stringtoBitmap(String string) { // 将字符串转换成Bitmap类型 Bitmap bitmap = null ; try { byte [] bitmapArray; bitmapArray = Base64.decode(string, Base64.DEFAULT); bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0 , bitmapArray.length); } catch (Exception e) { e.printStackTrace(); } return bitmap; } 12.将Bitmap转换成字符串 1 2 3 4 5 6 7 8 9 10 public static String bitmaptoString(Bitmap bitmap) { // 将Bitmap转换成字符串 String string = null ; ByteArrayOutputStream bStream = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 100 , bStream); byte [] bytes = bStream.toByteArray(); string = Base64.encodeToString(bytes, Base64.DEFAULT); return string; } 13.byte[]转为文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 //byte[]转为文件 public static File getFileFromBytes( byte [] b) { BufferedOutputStream stream = null ; File file = null ; try { // 判断SDcard状态 if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { // 错误提示 return null ; } // 检查SDcard空间 File SDCardRoot = Environment.getExternalStorageDirectory(); if (SDCardRoot.getFreeSpace() < 10000 ) { // 弹出对话框提示用户空间不够 Log.e( "Utils" , "存储空间不够" ); return null ; } // 在SDcard创建文件夹及文件 File bitmapFile = new File(SDCardRoot.getPath() + FILE_PATH); bitmapFile.getParentFile().mkdirs(); // 创建文件夹 FileOutputStream fstream = new FileOutputStream(bitmapFile); stream = new BufferedOutputStream(fstream); stream.write(b); } catch (Exception e) { e.printStackTrace(); } finally { if (stream != null ) { try { stream.close(); } catch (IOException e1) { e1.printStackTrace(); } } } return file; } 14.图片压缩 1 2 3 4 5 6 7 8 9 10 11 12 13 //图片缩放 public static Bitmap scaleDownBitmap(Bitmap photo, int newHeight, Context context) { final float densityMultiplier = context.getResources().getDisplayMetrics().density; int h = ( int ) (newHeight * densityMultiplier); int w = ( int ) (h * photo.getWidth() / (( double ) photo.getHeight())); photo = Bitmap.createScaledBitmap(photo, w, h, true ); return photo; } 15.将byte[]转换成InputStream 1 2 3 4 5 6 // 将byte[]转换成InputStream public InputStream Byte2InputStream( byte [] b) { ByteArrayInputStream bais = new ByteArrayInputStream(b); return bais; } 16.将InputStream转换成byte[] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // 将InputStream转换成byte[] public byte [] InputStream2Bytes(InputStream is) { String str = "" ; byte [] readByte = new byte [ 1024 ]; int readCount = - 1 ; try { while ((readCount = is.read(readByte, 0 , 1024 )) != - 1 ) { str += new String(readByte).trim(); } return str.getBytes(); } catch (Exception e) { e.printStackTrace(); } return null ; } 17.将Bitmap转换成InputStream 1 2 3 4 5 6 7 8 // 将Bitmap转换成InputStream public InputStream Bitmap2InputStream(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 100 , baos); InputStream is = new ByteArrayInputStream(baos.toByteArray()); return is; } 18.将Bitmap转换成InputStream 1 2 3 4 5 6 7 8 // 将Bitmap转换成InputStream public InputStream Bitmap2InputStream(Bitmap bm, int quality) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, quality, baos); InputStream is = new ByteArrayInputStream(baos.toByteArray()); return is; } 19.将InputStream转换成Bitmap 1 2 3 4 5 // 将InputStream转换成Bitmap public Bitmap InputStream2Bitmap(InputStream is) { return BitmapFactory.decodeStream(is); } 20.Drawable转换成InputStream 1 2 3 4 5 6 // Drawable转换成InputStream public InputStream Drawable2InputStream(Drawable d) { Bitmap bitmap = this .drawable2Bitmap(d); return this .Bitmap2InputStream(bitmap); } 21.InputStream转换成Drawable 1 2 3 4 5 6 // InputStream转换成Drawable public Drawable InputStream2Drawable(InputStream is) { Bitmap bitmap = this .InputStream2Bitmap(is); return this .bitmap2Drawable(bitmap); } 22.Drawable转换成byte[] 1 2 3 4 5 6 // Drawable转换成byte[] public byte [] Drawable2Bytes(Drawable d) { Bitmap bitmap = this .drawable2Bitmap(d); return this .Bitmap2Bytes(bitmap); } 23.byte[]转换成Drawable 1 2 3 4 5 6 // byte[]转换成Drawable public Drawable Bytes2Drawable( byte [] b) { Bitmap bitmap = this .Bytes2Bitmap(b); return this .bitmap2Drawable(bitmap); } 24.Drawable转换成Bitmap 1 2 3 4 5 6 7 8 9 10 // Drawable转换成Bitmap public Bitmap drawable2Bitmap(Drawable drawable) { Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); drawable.setBounds( 0 , 0 , drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; } 25.Bitmap转换成Drawable 1 2 3 4 5 6 7 // Bitmap转换成Drawable public Drawable bitmap2Drawable(Bitmap bitmap) { BitmapDrawable bd = new BitmapDrawable(bitmap); Drawable d = (Drawable) bd; return d; } 本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1304090,如需转载请自行联系原作者

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册