SparseArray instead HashMap Android example
Old code with HashMap
Lint warning:
Use new SparseArray(...) instead for better performance
Issue: Looks for opportunities to replace HashMaps with the more efficient SparseArray
Id: UseSparseArrays
New code with SparseArray
SparseArray methods:
//developer.android.com/reference/android/util/SparseArray.html
Map<Integer, Bitmap> _bitmapCache = new HashMap<Integer, Bitmap>();
private void fillBitmapCache() {
_bitmapCache.put(R.drawable.icon, BitmapFactory.decodeResource(getResources(), R.drawable.icon));
_bitmapCache.put(R.drawable.abstrakt, BitmapFactory.decodeResource(getResources(), R.drawable.abstrakt));
_bitmapCache.put(R.drawable.wallpaper, BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper));
_bitmapCache.put(R.drawable.scissors, BitmapFactory.decodeResource(getResources(),
}
Bitmap bm = _bitmapCache.get(R.drawable.icon);
Lint warning:
Use new SparseArray
Issue: Looks for opportunities to replace HashMaps with the more efficient SparseArray
Id: UseSparseArrays
New code with SparseArray
SparseArray<Bitmap> _bitmapCache = new SparseArray<Bitmap>();
private void fillBitmapCache() {
_bitmapCache.put(R.drawable.icon, BitmapFactory.decodeResource(getResources(), R.drawable.icon));
_bitmapCache.put(R.drawable.abstrakt, BitmapFactory.decodeResource(getResources(), R.drawable.abstrakt));
_bitmapCache.put(R.drawable.wallpaper, BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper));
_bitmapCache.put(R.drawable.scissors, BitmapFactory.decodeResource(getResources(),
}
Bitmap bm = _bitmapCache.get(R.drawable.icon);
SparseArray methods:
//developer.android.com/reference/android/util/SparseArray.html
396LW NO topic_id
AD
Další témata ....(Topics)
Close one project:¨
-right click on project
- select Close project
Close more projects:
- right click on project
- select Close Unrelated Projects
Hide closed projects:
- package explorer
- view menu
- select Closed projects
-right click on project
- select Close project
Close more projects:
- right click on project
- select Close Unrelated Projects
Hide closed projects:
- package explorer
- view menu
- select Closed projects
How change background color of View Android sample.
MainActivity.java
main.xml
MainActivity.java
public class MainActivity extends Activity {
TextView hTextView;
TableRow hTableRow;
Button hButton, hButtonStop;
private Handler mHandler = new Handler();
private int nCounter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hTextView = (TextView)findViewById(R.id.idTextView);
hButton = (Button)findViewById(R.id.idButton);
hButton.setOnClickListener(mButtonStartListener);
hTableRow = (TableRow)findViewById(R.id.idTableRow1);
} // end onCreate
View.OnClickListener mButtonStartListener = new OnClickListener() {
public void onClick(View v) {
hTableRow.setBackgroundColor(Color.YELLOW);
}
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/tableLayout1">
<TableRow android:id="@+id/idTableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#5655AA">
<TextView
android:id="@+id/idTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</TableRow>
</TableLayout>
</LinearLayout>
1.) Try reopen the Emulaor and restart Eclipse.
OR
2.) Try to delete AVD from Eclipse menu Window - AVD manager.
OR
3.) Insert into manifest.xml this source code.
OR
2.) Try to delete AVD from Eclipse menu Window - AVD manager.
OR
3.) Insert into manifest.xml this source code.
<manifest xmlns:android="//schemas.android.com/apk/res/android"
package="com.myweb.mypackage"
android:installLocation="preferExternal"
Cut, shear, clip, snip, crop a bitmap, picture, image Android example
public class ApokusActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static class SampleView extends View {
// CONSTRUCTOR
public SampleView(Context context) {
super(context);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
canvas.drawColor(Color.YELLOW);
// you need to insert a image flower_blue into res/drawable folder
paint.setFilterBitmap(true);
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.flower_blue);
Bitmap croppedBmp = Bitmap.createBitmap(bitmapOrg, 0, 0,
bitmapOrg.getWidth() / 2, bitmapOrg.getHeight());
int h = bitmapOrg.getHeight();
canvas.drawBitmap(bitmapOrg, 10, 10, paint);
canvas.drawBitmap(croppedBmp, 10, 10 + h + 10, paint);
}
}
}
Screen Android example source code for developers.
Get orientation of screen.
Get orientation of screen.
public int getScreenOrientation() {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
return 1; // Portrait Mode
}else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
return 2; // Landscape mode
}
return 0;
}
Editace: 2013-12-09 13:04:15
Počet článků v kategorii: 396
Url:sparsearray-instead-hashmap-android-example