Android_AsyncTaskDemo之QQ记步数(画圆形图片知识)
今天学习了AsyncTask Android 的异步机制。我简单的实现我的一个小小案例——qq记步数。然后穿插一个画圆形图片的知识点。
由于所学知识有限,目前我计数,还有排名等等我就简单的利用随机数实现。多有不是之处见谅啊。
我们的xml layout布局文件
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context="com.example.qqsport.MainActivity" > 7 <!-- 头部 --> 8 <TextView 9 android:id="@+id/name" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:gravity="center" 13 android:layout_marginTop="10dp" 14 android:textSize="20sp" 15 android:text="...heyhhz...." /> 16 <LinearLayout 17 android:layout_width="match_parent" 18 android:layout_height="match_parent" 19 android:orientation="horizontal" 20 android:layout_marginTop="10dp" 21 android:layout_marginLeft="45dp"> 22 <!-- 附近排名 --> 23 <LinearLayout 24 android:layout_weight="1" 25 android:orientation="vertical" 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content"> 28 29 <TextView 30 android:id="@+id/scort" 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" 33 android:text="附近排名" /> 34 35 <TextView 36 android:id="@+id/fujin" 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:layout_marginTop="30dp" 40 android:text="第0名" /> 41 </LinearLayout> 42 <!-- 头像 --> 43 <LinearLayout 44 android:layout_weight="1" 45 android:orientation="vertical" 46 android:layout_width="wrap_content" 47 android:layout_height="wrap_content"> 48 <ImageView 49 android:id="@+id/face" 50 android:layout_width="wrap_content" 51 android:layout_height="wrap_content" 52 android:src="@drawable/face"/> 53 <TextView 54 android:id="@+id/bushu" 55 android:layout_width="wrap_content" 56 android:layout_height="wrap_content" 57 android:layout_marginTop="5dp"/> 58 </LinearLayout> 59 <!-- 排行榜 --> 60 <LinearLayout 61 android:layout_weight="1" 62 android:orientation="vertical" 63 android:layout_width="wrap_content" 64 android:layout_height="wrap_content"> 65 <TextView 66 android:layout_width="wrap_content" 67 android:layout_height="wrap_content" 68 android:id="@+id/list" 69 android:text="排行榜"/> 70 <TextView 71 android:id="@+id/place" 72 android:layout_width="wrap_content" 73 android:layout_height="wrap_content" 74 android:text="第0名" 75 android:layout_marginTop="30dp"/> 76 </LinearLayout> 77 </LinearLayout> 78 79 </LinearLayout>
我们的MainActivity.class 文件 其中我们头像变成圆形的代码也在其中。
package com.example.qqsport;/*** 1.写一个随机数充当 该用户的步数* 2.实现 从1加到 该随机数的效果* */import java.util.Random;import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.os.Bundle; import android.util.Log; import android.widget.ImageView; import android.widget.TextView;public class MainActivity extends Activity {private static final String TAG = "RoundImage";private ImageView mImg;private TextView tv_bushu,tv_place,tv_fujin;private Random r = new Random();private MyTask myTask = new MyTask(this);@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);huatu();int bushu = r.nextInt(5000)+5000;//随机设置排名int place = r.nextInt(50);//设置随机数 附近排名int fujin = r.nextInt(200);//初始化步数tv_bushu = (TextView) findViewById(R.id.bushu);//初始化 排行榜控件 tv_place = (TextView) findViewById(R.id.place);//附近tv_fujin = (TextView) findViewById(R.id.fujin);myTask.execute(tv_bushu,bushu,tv_place,place,tv_fujin,fujin);}//画圆形图片private void huatu() {//初始化控件mImg = (ImageView) findViewById(R.id.face);//裁剪图片BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeResource(getResources(), R.drawable.face, options);Log.d(TAG, "original outwidth:"+options.outWidth);//此宽度是目标 imageView 希望的大小,你可以自定义imageView 然后获得ImageView 的宽度int dstWidth = 100;//我们需要加载的图片可能很大,我们先对原有的图片进行裁剪int sampleSize = calculateInSampleSize(options, dstWidth, dstWidth);options.inSampleSize = sampleSize;options.inJustDecodeBounds = false;Log.d(TAG, "sample size:" + sampleSize);Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.face, options);//绘制图片Bitmap resultBmp = Bitmap.createBitmap(dstWidth, dstWidth, Bitmap.Config.ARGB_8888);Paint paint = new Paint();paint.setAntiAlias(true);Canvas canvas = new Canvas(resultBmp);//画图canvas.drawCircle(dstWidth / 2, dstWidth / 2, dstWidth / 2, paint);//选择交集去上层图片paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));canvas.drawBitmap(bmp, new Rect(0, 0, bmp.getWidth(), bmp.getWidth()), new Rect(0, 0, dstWidth, dstWidth), paint);mImg.setImageBitmap(resultBmp);bmp.recycle();}private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {// Raw height and width of imagefinal int height = options.outHeight;final int width = options.outWidth;int inSampleSize = 1;if( height > reqHeight || width > reqWidth) {final int halfHeight = height / 2;final int halfWidth = width / 2;// Calculate the largest inSampleSize value that is a power of 2 and// keeps both// height and width larger than the requested height and width.while ((halfHeight / inSampleSize) > reqHeight && ( halfWidth / inSampleSize) > reqWidth) {inSampleSize *= 2;}}return inSampleSize;}}
接下来是我们后台加载,异步机制。前面也说了,小编在这是利用随机数进行的,然后在这里加载出来的数据由于是同时传入的,他按照先后顺序进行显示,并没有实现同步的一个操作。这里要大家自己去实现下啦。
1 package com.example.qqsport; 2 3 import android.app.Activity; 4 import android.os.AsyncTask; 5 import android.widget.TextView; 6 import android.widget.Toast; 7 8 public class MyTask extends AsyncTask{ 9 10 private Activity runActivit; 11 12 private TextView tv_bs,tv_fj,tv_pla; 13 private int bs,pla,fj; 14 public MyTask (Activity activity){ 15 this.runActivit = activity; 16 } 17 18 19 @Override 20 protected Object doInBackground(Object... params) { 21 //我的步数 22 tv_bs = (TextView) params[0]; 23 bs = (Integer) params[1]; 24 for(int i = 1; i <= bs; i++) { 25 26 publishProgress(i,1); 27 } 28 //排名 29 tv_pla = (TextView) params[2]; 30 pla = (Integer) params[3]; 31 for (int i = 1; i <= pla; i++){ 32 publishProgress(i,2); 33 34 } 35 //附近排名 36 tv_fj = (TextView) params[4]; 37 fj = (Integer) params[5]; 38 for (int i = 1; i <= fj; i++){ 39 publishProgress(i,3); 40 41 } 42 return "加载完成"; 43 } 44 45 //onPostExecute 后台数据结束后调用的方法 46 @Override 47 protected void onPostExecute(Object result) { 48 if( bs > 7000) { 49 Toast.makeText(runActivit, "哎哟,不错哟今天", 1).show(); 50 }else { 51 Toast.makeText(runActivit, "偶尔放慢脚步可以思考人生", 1).show(); 52 } 53 54 } 55 56 //onProgressUpdate 当前面使用了publishProgress 这个方法的时候就调用。 57 @Override 58 protected void onProgressUpdate(Object... values) { 59 Integer aa = (Integer) values[0]; 60 Integer bb = (Integer) values[1]; 61 if(bb == 1){ 62 tv_bs.setText(aa + ""); 63 } 64 if(bb == 2){ 65 66 tv_pla.setText("第"+aa + "名"); 67 } 68 if(bb == 3){ 69 70 tv_fj.setText("第"+aa + "名"); 71 } 72 73 74 } 75 76 77 78 79 80 }
大家可以拷贝代码自己试下。[微笑]欢迎大家交流指教,我也是初学者。
转载于:https://www.cnblogs.com/heyhhz/p/6135366.html
Android_AsyncTaskDemo之QQ记步数(画圆形图片知识)相关推荐
- Kotlin实战练习——自定义圆形图片三种实现方式
Kotlin实战练习--自定义圆形图片三种实现方式 前言 如今Kotlin越来越重要,本人也开始了Kotlin的学习.为了检测学习效果,加深学习印象,同时回顾一下以前的一些知识点,决定从写一个自定义圆 ...
- Java将图片处理成背景透明的圆形图片
/** @author Michael Feng* @date 2017年9月4日*/import java.awt.AlphaComposite; import java.awt.Color; im ...
- Android画各种圆,饼图,环图,圆形图片
最近在学习android的画图所以就学习了一下.看了很多资料.特别是爱哥的博客,学习了很多,我也要分享一下: 首先画个圆(简单的很): 自定义的View 设置好paint,canvas.drawCir ...
- android画一个圆形图片组件
imageview 显示圆形图片如下图. 圆形头像原理. 1.根据图片创建一个大小相同的画布. 2.在画布上画一个圆形. 3.画一个绘制交集,显示上层. // 将圆形图片,返回Bitmappublic ...
- 用php画一个蓝底红色的圆_PHP 画出 透明背景 的 圆形 图片程序
PHP 想要用 GD 来画出圆形.椭圆形等等的图形,该怎么画呢?背景想要是透明的,要怎么做呢? PHP 画出 透明背景 的 圆形 图片程序 这边来示范下述: 图片画出 圆形 和 椭圆形 画两个不同的圆 ...
- iOS自定义裁剪区域,正方形圆形图片头像裁剪,仿QQ头像裁剪,圆形遮罩,矩型遮罩
最近项目中用到了自定义图片裁剪区域的图片裁剪功能,自己写了一个,可能有诸多不完善的地方,请大家指正. 支持任意区域裁剪,9:16裁剪.16:9裁剪.1:1裁剪.圆形裁剪等等,总之裁剪框的大小,裁剪框的 ...
- Android自定义View之仿QQ运动步数进度效果
文章目录 前言 先看效果图 ![在这里插入图片描述](https://img-blog.csdnimg.cn/6e4ddec17933496ea4830fa08d8ffbe5.png?x-oss-pr ...
- android 自定义园动画,Android 自定View实现仿QQ运动步数圆弧及动画效果
在之前的Android超精准计步器开发-Dylan计步中的首页用到了一个自定义控件,和QQ运动的界面有点类似,还有动画效果,下面就来讲一下这个View是如何绘制的. 1.先看效果图 2.效果图分析 功 ...
- Android 自定义圆形图片 CircleImageView
1.效果预览 1.1.布局中写自定义圆形图片的路径即可 1.2.然后看一看图片效果 1.3.原图是这样的 @mipmap/ic_launcher 2.使用过程 2.1.CircleImageView源 ...
最新文章
- 学会Python后能找到什么工作,待遇如何?
- JS获取URL中参数值(QueryString)的4种方法分享
- 为什么密码比字符串更喜欢char []?
- 用到的 git 命令
- 简单的选择排序(内部排序)
- linux cacti安装教程,Linux下cacti的安装与配置
- 原来女孩子做电子工程师也很厉害。。
- C++实现线段树求区间和-区间查询
- ehcache rmi_EhCache复制:RMI与JGroups
- java初学者指南_Java初学者指南
- ldap认证服务的搭建
- Linux 下安装JDK1.8-解压版
- 毕设题目:Matlab语音处理
- 体育类App原型制作分享-Onefootball
- 第五届蓝桥杯C++B组:史丰收速算
- zip压缩文件加密码以及Office文件打开需要密码
- 逆向webpack打包,还原出原始文件。
- Detectron2迷幻问题解答 - 持续连载
- 将element-plus分页组件由默认英文,改为中文
- 在线等价类与离线等价类(概念)