提交 75c4ddac 编写于 作者: 门心叼龙's avatar 门心叼龙

code perfect

上级 2ad2f85a
...@@ -12,7 +12,8 @@ ...@@ -12,7 +12,8 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity android:name=".TestAsyncTaskActivity"></activity> <activity android:name=".bitmap.test.TestCacheActivity"></activity>
<activity android:name=".TestAsyncTaskActivity" />
<provider <provider
android:name=".provider.MyContentProvider" android:name=".provider.MyContentProvider"
...@@ -53,7 +54,7 @@ ...@@ -53,7 +54,7 @@
<activity android:name=".HorizontalScrollViewExActivity" /> <activity android:name=".HorizontalScrollViewExActivity" />
<service android:name=".service.TestService" /> <service android:name=".service.TestService" />
<service android:name=".thread.MyIntentService"/> <service android:name=".thread.MyIntentService" />
</application> </application>
</manifest> </manifest>
\ No newline at end of file
...@@ -11,6 +11,7 @@ import android.view.View; ...@@ -11,6 +11,7 @@ import android.view.View;
import android.widget.Button; import android.widget.Button;
import com.mxdl.customview.bitmap.DiskLruCache; import com.mxdl.customview.bitmap.DiskLruCache;
import com.mxdl.customview.bitmap.test.TestCacheActivity;
import com.mxdl.customview.test.MainTestActivity; import com.mxdl.customview.test.MainTestActivity;
import com.mxdl.customview.thread.MyAsyncTask; import com.mxdl.customview.thread.MyAsyncTask;
...@@ -34,6 +35,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -34,6 +35,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
private Button mBtnRectCapture; private Button mBtnRectCapture;
private Button mBtnService; private Button mBtnService;
private Button mBtnAsyncTask; private Button mBtnAsyncTask;
private Button mBtnCache;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
...@@ -47,6 +49,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -47,6 +49,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
mBtnRectCapture = findViewById(R.id.btn_capture_rect); mBtnRectCapture = findViewById(R.id.btn_capture_rect);
mBtnService = findViewById(R.id.btn_service); mBtnService = findViewById(R.id.btn_service);
mBtnAsyncTask = findViewById(R.id.btn_async_task); mBtnAsyncTask = findViewById(R.id.btn_async_task);
mBtnCache = findViewById(R.id.btn_cache);
mBtnStickyLayout.setOnClickListener(this); mBtnStickyLayout.setOnClickListener(this);
mBtnHorizontalScrollViewEx.setOnClickListener(this); mBtnHorizontalScrollViewEx.setOnClickListener(this);
...@@ -56,12 +59,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -56,12 +59,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
mBtnRectCapture.setOnClickListener(this); mBtnRectCapture.setOnClickListener(this);
mBtnService.setOnClickListener(this); mBtnService.setOnClickListener(this);
mBtnAsyncTask.setOnClickListener(this); mBtnAsyncTask.setOnClickListener(this);
mBtnCache.setOnClickListener(this);
final long DISK_CACHE_SIZE = 1024 * 1024 * 50; //50MB
File diskCacheDir = getBaseContext().getCacheDir();
if (!diskCacheDir.exists()) {
diskCacheDir.mkdirs();
}
} }
...@@ -92,6 +90,9 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -92,6 +90,9 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
case R.id.btn_async_task: case R.id.btn_async_task:
startActivity(new Intent(this, TestAsyncTaskActivity.class)); startActivity(new Intent(this, TestAsyncTaskActivity.class));
break; break;
case R.id.btn_cache:
startActivity(new Intent(this, TestCacheActivity.class));
break;
} }
......
package com.mxdl.customview.bitmap;
import java.io.FileDescriptor;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
public class ImageResizer {
private static final String TAG = "ImageResizer";
public ImageResizer() {
}
public Bitmap decodeSampledBitmapFromResource(Resources res,
int resId, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public void test(){
}
public Bitmap decodeSampledBitmapFromFileDescriptor(FileDescriptor fd, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fd, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFileDescriptor(fd, null, options);
}
public int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
if (reqWidth == 0 || reqHeight == 0) {
return 1;
}
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
Log.d(TAG, "origin, w= " + width + " h=" + height);
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;
}
}
Log.d(TAG, "sampleSize:" + inSampleSize);
return inSampleSize;
}
}
package com.mxdl.customview.bitmap.test;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import android.util.LruCache;
import com.mxdl.customview.bitmap.DiskLruCache;
import com.mxdl.customview.bitmap.ImageResizer;
import com.mxdl.customview.bitmap.test.entity.Person;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/**
* Description: <Test><br>
* Author: mxdl<br>
* Date: 2019/11/28<br>
* Version: V1.0.0<br>
* Update: <br>
*/
public class Test {
private DiskLruCache mDiskLruCache;
public static void main(String[] args) {
//test();
//test2();
}
private void test2() {
File forder = Environment.getDownloadCacheDirectory();
try {
mDiskLruCache = DiskLruCache.open(forder, 100, 1, 50 * 1024 * 1024);
DiskLruCache.Editor editor = mDiskLruCache.edit("mxdl");
if (editor != null) {
OutputStream outputStream = editor.newOutputStream(0);
if (downloadUrlToStream("mxdl", outputStream)) {
editor.commit();
} else {
editor.abort();
}
mDiskLruCache.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void getFile(String url){
Bitmap bitmap = null;
String key = hashKeyFormUrl(url);
DiskLruCache.Snapshot snapShot = null;
try {
snapShot = mDiskLruCache.get(key);
if (snapShot != null) {
FileInputStream fileInputStream = (FileInputStream)snapShot.getInputStream(0);
FileDescriptor fileDescriptor = fileInputStream.getFD();
bitmap = new ImageResizer().decodeSampledBitmapFromFileDescriptor(fileDescriptor,100,100);
if (bitmap != null) {
//addBitmapToMemoryCache(key,bitmap);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private String hashKeyFormUrl(String url) {
String cacheKey;
try {
final MessageDigest mDigest = MessageDigest.getInstance("MD5");
mDigest.update(url.getBytes());
cacheKey = bytesToHexString(mDigest.digest());
} catch (NoSuchAlgorithmException e) {
cacheKey = String.valueOf(url.hashCode());
}
return cacheKey;
}
private String bytesToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
return sb.toString();
}
public boolean downloadUrlToStream(String urlString, OutputStream outputStream) {
HttpURLConnection urlConnection = null;
BufferedOutputStream out = null;
BufferedInputStream in = null;
try {
final URL url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
in = new BufferedInputStream(urlConnection.getInputStream(), 1024 * 1);
out = new BufferedOutputStream(outputStream, 1024 * 1);
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
return true;
} catch (IOException e) {
Log.e("MYTAG", "downloadBitmap failed." + e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
//MyUtils.close(out);
//MyUtils.close(in);
}
return false;
}
private static void test() {
HashMap<String, String> map1 = new HashMap<>();
map1.put("zhangsan", "zhangsan");
map1.put("lisi", "lisi");
map1.put("wangwu", "wangwu");
Set<Map.Entry<String, String>> entrySet1 = map1.entrySet();
Iterator<Map.Entry<String, String>> iterator1 = entrySet1.iterator();
while (iterator1.hasNext()) {
System.out.println(iterator1.next().toString());
}
System.out.println("==========================================");
LinkedHashMap<String, String> map = new LinkedHashMap<>();
map.put("zhangsan", "zhangsan");
map.put("lisi", "lisi");
map.put("wangwu", "wangwu");
String wangwu = map.get("zhangsan");
String put = map.put("zhaoliu", "zhaoliu");
Set<Map.Entry<String, String>> entrySet = map.entrySet();
Iterator<Map.Entry<String, String>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().toString());
}
}
}
package com.mxdl.customview.bitmap.test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.LruCache;
import com.mxdl.customview.R;
import com.mxdl.customview.bitmap.test.entity.Person;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class TestCacheActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_cache);
long maxMemory = Runtime.getRuntime().maxMemory();
Log.v("MYTAG","maxMemary:"+maxMemory+"b");
Log.v("MYTAG","maxMemary:"+maxMemory/(1024)+"kb");
Log.v("MYTAG","maxMemary:"+maxMemory/(1024*1024)+"mb");
LruCache<String, Person> cache = new LruCache<>(3);
cache.put("0",new Person("0,",19));
cache.put("1",new Person("1,",19));
cache.put("2",new Person("2,",19));
Person person = cache.get("0");
//cache.put("3",new Person("3,",19));
LinkedHashMap<String, Person> linkedHashMap = (LinkedHashMap<String, Person>) cache.snapshot();
Set<String> set = linkedHashMap.keySet();
Iterator<String> iterator = set.iterator();
while(iterator.hasNext()){
Log.v("MYTAG",linkedHashMap.get(iterator.next()).toString());
}
Log.v("MYTAG","===============================-------------------------");
LruCache<String,String> map = new LruCache<>(3);
map.put("zhangsan","zhangsan");
map.put("lisi","lisi");
map.put("wangwu","wangwu");
String wangwu = map.get("zhangsan");
String put = map.put("zaholiu", "zhaoliou");
String put1 = map.put("zaholiu", "zhaoliou");
Log.v("MYTAG",put == null ? "null":"not null");
Log.v("MYTAG",put1 == null ? "null":"not null");
Set<Map.Entry<String, String>> entrySet = map.snapshot().entrySet();
Iterator<Map.Entry<String, String>> iterator1 = entrySet.iterator();
while(iterator1.hasNext()){
Log.v("MYTAG",iterator1.next().toString());
}
}
}
package com.mxdl.customview.bitmap.test.entity;
import androidx.annotation.NonNull;
/**
* Description: <Person><br>
* Author: mxdl<br>
* Date: 2019/11/28<br>
* Version: V1.0.0<br>
* Update: <br>
*/
public class Person {
public String name;
public int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" + "name='" + name + '\'' + ", age=" + age + '}';
}
}
...@@ -62,5 +62,11 @@ ...@@ -62,5 +62,11 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="TestAsyncTask" android:text="TestAsyncTask"
android:textAllCaps="false" /> android:textAllCaps="false" />
<Button
android:id="@+id/btn_cache"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cache"
android:textAllCaps="false" />
</LinearLayout> </LinearLayout>
</androidx.core.widget.NestedScrollView> </androidx.core.widget.NestedScrollView>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".bitmap.test.TestCacheActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册