Android_AsyncTaskDemo之QQ记步数(画圆形图片知识)
今天学习了AsyncTask Android 的异步机制。我简单的实现我的一个小小案例——qq记步数。然后穿插一个画圆形图片的知识点。
由于所学知识有限,目前我计数,还有排名等等我就简单的利用随机数实现。多有不是之处见谅啊。
我们的xml layout布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.qqsport.MainActivity" >
<!-- 头部 -->
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:text="...heyhhz...." />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginLeft="45dp">
<!-- 附近排名 -->
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"> <TextView
android:id="@+id/scort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="附近排名" /> <TextView
android:id="@+id/fujin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="第0名" />
</LinearLayout>
<!-- 头像 -->
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/face"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/face"/>
<TextView
android:id="@+id/bushu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"/>
</LinearLayout>
<!-- 排行榜 -->
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list"
android:text="排行榜"/>
<TextView
android:id="@+id/place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第0名"
android:layout_marginTop="30dp"/>
</LinearLayout>
</LinearLayout> </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); @Override
protected 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 image
final 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;
} }
接下来是我们后台加载,异步机制。前面也说了,小编在这是利用随机数进行的,然后在这里加载出来的数据由于是同时传入的,他按照先后顺序进行显示,并没有实现同步的一个操作。这里要大家自己去实现下啦。
package com.example.qqsport; import android.app.Activity;
import android.os.AsyncTask;
import android.widget.TextView;
import android.widget.Toast; public class MyTask extends AsyncTask{ private Activity runActivit; private TextView tv_bs,tv_fj,tv_pla;
private int bs,pla,fj;
public MyTask (Activity activity){
this.runActivit = activity;
} @Override
protected Object doInBackground(Object... params) {
//我的步数
tv_bs = (TextView) params[0];
bs = (Integer) params[1];
for(int i = 1; i <= bs; i++) { publishProgress(i,1);
}
//排名
tv_pla = (TextView) params[2];
pla = (Integer) params[3];
for (int i = 1; i <= pla; i++){
publishProgress(i,2); }
//附近排名
tv_fj = (TextView) params[4];
fj = (Integer) params[5];
for (int i = 1; i <= fj; i++){
publishProgress(i,3); }
return "加载完成";
} //onPostExecute 后台数据结束后调用的方法
@Override
protected void onPostExecute(Object result) {
if( bs > 7000) {
Toast.makeText(runActivit, "哎哟,不错哟今天", 1).show();
}else {
Toast.makeText(runActivit, "偶尔放慢脚步可以思考人生", 1).show();
} } //onProgressUpdate 当前面使用了publishProgress 这个方法的时候就调用。
@Override
protected void onProgressUpdate(Object... values) {
Integer aa = (Integer) values[0];
Integer bb = (Integer) values[1];
if(bb == 1){
tv_bs.setText(aa + "");
}
if(bb == 2){ tv_pla.setText("第"+aa + "名");
}
if(bb == 3){ tv_fj.setText("第"+aa + "名");
} } }


大家可以拷贝代码自己试下。[微笑]欢迎大家交流指教,我也是初学者。
Android_AsyncTaskDemo之QQ记步数(画圆形图片知识)的更多相关文章
- 在android中画圆形图片的几种办法
在开发中常常会有一些需求,比方显示头像,显示一些特殊的需求,将图片显示成圆角或者圆形或者其它的一些形状. 可是往往我们手上的图片或者从server获取到的图片都是方形的.这时候就须要我们自己进行处理, ...
- android绘制圆形图片的两种方式
看下效果先 下面有完整的示例代码 使用BitmapShader(着色器) 我们在绘制view 的时候 就是小学上美术课 用水彩笔在本子上画画 使用着色器绘制圆形图片最简单的理解方式 就是把bitmap ...
- iOS常见用户头像的圆形图片裁剪常见的几种方法
在开发中,基本上APP的用户头像的处理都需要把用户所上传的方形图片,处理为圆形图片.在这里就总结三种常见的处理圆形图片的方法. 1.使用位图上下文 2.使用UIView的layer进行处理 3.使用r ...
- Android实现圆形图片
情景再现: 写Android程序也有一段时间了,今天突然被问怎么实现一个圆形图片,很多app图像是圆形的.但是用户上传的图像可不是圆的,所以问题就来了,需要我们代码实现圆形图片.但是大脑飞转想到第三 ...
- UIImage类扩展返回一个带边框的圆形图片
/** * 将image转换为圆型带边框的图片(最好写一个UIImage的类扩展) * * @param name 图片的名字 * @param borderWidth 外层边框的宽度 * @para ...
- Android圆形图片--ImageView
[ RoundImageView.java ] package com.dxd.roundimageview; import android.content.Context; import andro ...
- iOS开发——图形编程Swift篇&CAShapeLayer实现圆形图片加载动画
CAShapeLayer实现圆形图片加载动画 几个星期之前,Michael Villar在Motion试验中创建一个非常有趣的加载动画. 下面的GIF图片展示这个加载动画,它将一个圆形进度指示器和圆形 ...
- 使用CAShapeLayer来实现圆形图片加载动画[译]
原文链接 : How To Implement A Circular Image Loader Animation with CAShapeLayer 原文作者 : Rounak Jain 译文出自 ...
- Android圆形图片自定义控件
Android圆形图片控件效果图如下: 代码如下: RoundImageView.java package com.dxd.roundimageview; import android.content ...
随机推荐
- Git使用培训
1.VS“扩展和更新”菜单,NuGet 2.通过“扩展和更新”,联机搜索git,安装“Git Extensions”和“Git Source Control Provider” 3.通过Git命令配置 ...
- Sql Server 2008和2000查询表的字段和注释
-- SQL Server 2008 SELECT 表名 = d.name, 表说明 = case when a.colorder=1 then isnull(f.value,'') else '' ...
- Map接口
Map实现的包括HashMap 和TreeMap .建议使用HashMap ,效率更高.并且允许使用null值,单是必须保证键的唯一性,TreeMap不允许有空.在添加删除和定位映射关系的时候不如Ha ...
- rpm命令
RPM 安装.卸载.升级.查询和验证. RPM 安装 命令: rpm -i 文件名 如: rpm -i example.rpm 安装 example.rpm 包: rpm -iv example.rp ...
- 黑马程序员_ C语言基础之指针(三)
------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 概览 指针是C语言的精髓,但是很多初学者往往对于指针的概念并不深刻,以至于学完之后随着时间的推移 ...
- 慕课网-Java入门第一季-6-8 使用 foreach 操作数组
来源:http://www.imooc.com/code/1864 foreach 并不是 Java 中的关键字,是 for 语句的特殊简化版本,在遍历数组.集合时, foreach 更简单便捷.从英 ...
- python2.7.9基础学习
一. 基 础 python python开头两行注释代码意义: #!/usr/bin/python 是用来说明脚本语言是python的,是要用/usr/bin下面的程序(工具)py ...
- 基于mini2440的Tslib的移植
软件平台: win7系统,虚拟机ubuntu12.04 mini2440开发板 tslib是电阻式触摸屏用于校准的一个软件库,是一个开源的程序,能够为触摸屏驱动获得的采样提供诸如滤波.去抖.校准等功能 ...
- Linux CentOS安装postgresql 9.4
一.前言 PostgreSQL通常也简称Postgres,是一个关系型数据库管理系统,适用于各种Linux操作系统.Windows.Solaris.BSD和Mac OS X.PostgreSQL遵循P ...
- CHARINDEX
实现查询条件多个值的或的关系 Select Id,Name from CustTable where CharIndex( CustTable.Name, 'ACDE,BEX,CCC')>0 C ...