Created
October 13, 2014 05:31
-
-
Save ssd863419/5b958d5996d7fa80aeab to your computer and use it in GitHub Desktop.
Android 使用長按選單取得Drawable圖片的寬高, ContextMenu, Bitmap, BitmapFactory.decodeResource()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.administrator.test; | |
import android.app.Activity; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.os.Bundle; | |
import android.view.ContextMenu; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.ImageView; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
/** | |
* Created by Administrator on 2014/10/12. | |
*/ | |
public class EX0701 extends Activity { | |
private TextView mTextView; | |
private ImageView mImageView; | |
/* 宣告Context Menu的選項常量 */ | |
protected final static int CONTEXT_ITEM1 = Menu.FIRST; | |
protected final static int CONTEXT_ITEM2 = Menu.FIRST + 1; | |
protected final static int CONTEXT_ITEM3 = Menu.FIRST + 2; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.ex0701); | |
mTextView = (TextView) findViewById(R.id.myTextView); | |
mImageView = (ImageView) findViewById(R.id.myImageView); | |
/* 將Drawable中的圖片, 放入自定義的ImageView中 */ | |
mImageView.setImageDrawable(getResources().getDrawable(R.drawable.ssd_poker_ace)); | |
/* 設置OnCreateContextMenuListener給TextView | |
* 在圖片上, 使用ContextMenu */ | |
mImageView.setOnCreateContextMenuListener(new ListView.OnCreateContextMenuListener() { | |
@Override | |
public void onCreateContextMenu( | |
ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { | |
menu.add(Menu.NONE, CONTEXT_ITEM1, 0, "取得圖片寬度"); | |
menu.add(Menu.NONE, CONTEXT_ITEM2, 0, "取得圖片高度"); | |
menu.add(Menu.NONE, CONTEXT_ITEM3, 0, "取得圖片寬高"); | |
} | |
}); | |
} | |
/* 重寫OnContextItemSelected來定義用戶單擊menu後的動作 */ | |
@Override | |
public boolean onContextItemSelected(MenuItem item) { | |
/* 自定義Bitmap對象, 並通過BitmapFactory.decodeResource取得 | |
* 預先Import至 Drawable的圖片*/ | |
Bitmap myBmp = BitmapFactory.decodeResource(getResources(), R.drawable.ssd_poker_ace); | |
int intHeight = myBmp.getHeight(); | |
int intWidth = myBmp.getWidth(); | |
String strOpt; | |
try { | |
/* 菜單選項與動作 */ | |
switch (item.getItemId()) { | |
/* 顯示寬度 */ | |
case CONTEXT_ITEM1: | |
strOpt = "寬度 = " + Integer.toString(intWidth); | |
mTextView.setText(strOpt); | |
break; | |
case CONTEXT_ITEM2: | |
strOpt = "高度 = " + Integer.toString(intHeight); | |
mTextView.setText(strOpt); | |
break; | |
case CONTEXT_ITEM3: | |
strOpt = "寬度 = " + Integer.toString(intWidth) + "\n"; | |
strOpt += "高度 = " + Integer.toString(intHeight); | |
mTextView.setText(strOpt); | |
break; | |
default: | |
break; | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return super.onContextItemSelected(item); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="New Text" | |
android:id="@+id/myTextView" /> | |
<ImageView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/myImageView" /> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment