upload android base code part7
|
@ -0,0 +1,39 @@
|
|||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.example.android.bitmapallocation"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0" >
|
||||
|
||||
<uses-sdk
|
||||
android:minSdkVersion="15"
|
||||
android:targetSdkVersion="15" />
|
||||
|
||||
<application
|
||||
android:icon="@drawable/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/AppTheme" >
|
||||
<activity
|
||||
android:name="com.example.android.bitmapallocation.BitmapAllocation"
|
||||
android:label="@string/title_activity_bitmap_allocation" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
After Width: | Height: | Size: 9.2 KiB |
After Width: | Height: | Size: 5.1 KiB |
After Width: | Height: | Size: 562 KiB |
After Width: | Height: | Size: 1.1 MiB |
After Width: | Height: | Size: 918 KiB |
After Width: | Height: | Size: 520 KiB |
After Width: | Height: | Size: 482 KiB |
After Width: | Height: | Size: 337 KiB |
After Width: | Height: | Size: 14 KiB |
|
@ -0,0 +1,38 @@
|
|||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" >
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/checkbox"
|
||||
android:text="@string/reuse_bitmap"
|
||||
android:checked="false"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
<TextView
|
||||
android:id="@+id/loadDuration"
|
||||
android:textSize="30sp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
<ImageView
|
||||
android:id="@+id/imageview"
|
||||
android:adjustViewBounds="true"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
</LinearLayout>
|
|
@ -0,0 +1,19 @@
|
|||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<resources>
|
||||
|
||||
<style name="AppTheme" parent="android:Theme.Holo.Light" />
|
||||
|
||||
</resources>
|
|
@ -0,0 +1,19 @@
|
|||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<resources>
|
||||
|
||||
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar" />
|
||||
|
||||
</resources>
|
|
@ -0,0 +1,21 @@
|
|||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<resources>
|
||||
|
||||
<string name="app_name">BitmapAllocation</string>
|
||||
<string name="title_activity_bitmap_allocation">BitmapAllocation</string>
|
||||
<string name="reuse_bitmap">Reuse Bitmap</string>
|
||||
|
||||
</resources>
|
|
@ -0,0 +1,19 @@
|
|||
<!-- Copyright (C) 2013 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<resources>
|
||||
|
||||
<style name="AppTheme" parent="android:Theme.Light" />
|
||||
|
||||
</resources>
|
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* Copyright (C) 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.bitmapallocation;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
/**
|
||||
* This example shows how to speed up bitmap loading and reduce garbage collection
|
||||
* by reusing existing bitmaps.
|
||||
*
|
||||
* Watch the associated video for this demo on the DevBytes channel of developer.android.com,
|
||||
* or on YouTube at https://www.youtube.com/watch?v=rsQet4nBVi8.
|
||||
*/
|
||||
public class BitmapAllocation extends Activity {
|
||||
|
||||
// There are some assumptions in this demo app that don't carry over well to the real world:
|
||||
// it assumes that all bitmaps are the same size and that loading all bitmaps as the activity
|
||||
// starts is good enough. A real application would be take a more flexible and robust
|
||||
// approach. But these assumptions are good enough for the purposes of this tutorial,
|
||||
// which is about reusing existing bitmaps of the same size.
|
||||
|
||||
int mCurrentIndex = 0;
|
||||
Bitmap mCurrentBitmap = null;
|
||||
BitmapFactory.Options mBitmapOptions;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_bitmap_allocation);
|
||||
|
||||
final int[] imageIDs = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
|
||||
R.drawable.e, R.drawable.f};
|
||||
|
||||
final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
|
||||
final TextView durationTextview = (TextView) findViewById(R.id.loadDuration);
|
||||
final ImageView imageview = (ImageView) findViewById(R.id.imageview);
|
||||
|
||||
// Create bitmap to be re-used, based on the size of one of the bitmaps
|
||||
mBitmapOptions = new BitmapFactory.Options();
|
||||
mBitmapOptions.inJustDecodeBounds = true;
|
||||
BitmapFactory.decodeResource(getResources(), R.drawable.a, mBitmapOptions);
|
||||
mCurrentBitmap = Bitmap.createBitmap(mBitmapOptions.outWidth,
|
||||
mBitmapOptions.outHeight, Bitmap.Config.ARGB_8888);
|
||||
mBitmapOptions.inJustDecodeBounds = false;
|
||||
mBitmapOptions.inBitmap = mCurrentBitmap;
|
||||
mBitmapOptions.inSampleSize = 1;
|
||||
BitmapFactory.decodeResource(getResources(), R.drawable.a, mBitmapOptions);
|
||||
imageview.setImageBitmap(mCurrentBitmap);
|
||||
|
||||
// When the user clicks on the image, load the next one in the list
|
||||
imageview.setOnClickListener(new View.OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
mCurrentIndex = (mCurrentIndex + 1) % imageIDs.length;
|
||||
BitmapFactory.Options bitmapOptions = null;
|
||||
if (checkbox.isChecked()) {
|
||||
// Re-use the bitmap by using BitmapOptions.inBitmap
|
||||
bitmapOptions = mBitmapOptions;
|
||||
bitmapOptions.inBitmap = mCurrentBitmap;
|
||||
}
|
||||
long startTime = System.currentTimeMillis();
|
||||
mCurrentBitmap = BitmapFactory.decodeResource(getResources(),
|
||||
imageIDs[mCurrentIndex], bitmapOptions);
|
||||
imageview.setImageBitmap(mCurrentBitmap);
|
||||
|
||||
// One way you can see the difference between reusing and not is through the
|
||||
// timing reported here. But you can also see a huge impact in the garbage
|
||||
// collector if you look at logcat with and without reuse. Avoiding garbage
|
||||
// collection when possible, especially for large items like bitmaps,
|
||||
// is always a good idea.
|
||||
durationTextview.setText("Load took " +
|
||||
(System.currentTimeMillis() - startTime));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|