androidsina 마이크로스피커 이모티콘 기능의 실현

2841 단어
이모티콘은 로컬 이모티콘으로 모든 이모티콘 그림을 gridview에 불러옵니다.그리고gridview의 클릭 이벤트에서 다음과 같이 처리합니다.
@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3) {
		if(mEditText == null) return;
		
		String content = mEditText.getText().toString().trim() + emotionNames[index];
		if(TextUtils.isEmpty(content)) return;
		
		Log.d(TAG, "SinaEmotionView: content = " + content);
		
		boolean isLeft = true;	//          
		List<Integer> leftBracketAt = new ArrayList<Integer>();
		List<Integer> rightBracketAt = new ArrayList<Integer>();
		
		char[] c_content = content.toCharArray();
		for(int i = 0; i < c_content.length; i++){
			if('[' == c_content[i]){
//				Log.d(TAG, "left ------- " + i);
				if(!isLeft){	//          ,   。      。
					leftBracketAt.remove(leftBracketAt.size() - 1);
				}
				leftBracketAt.add(new Integer(i));
				isLeft = false;
			}else if(']' == c_content[i]){
//				Log.d(TAG, "right ------- " + i);
				if(isLeft){
					rightBracketAt.remove(leftBracketAt.size() - 1);
				}
				rightBracketAt.add(new Integer(i));
				isLeft = true;
			}
		}
		
		SpannableString spannable = new SpannableString(content);
		//    []             //          "["    "]"
		int miniSize = leftBracketAt.size() <= rightBracketAt.size() ? leftBracketAt.size() : rightBracketAt.size();
		for(int j = 0; j < miniSize; j++){
			int leftAt = leftBracketAt.get(j);
			int rightAt = rightBracketAt.get(j) + 1;
			while(leftAt >= rightAt){
				rightBracketAt.remove(j);
				rightAt = rightBracketAt.get(j);
//				Log.d(TAG, "while ---- ");
			}
			String name = content.substring(leftAt, rightAt);
//			Log.d(TAG, "name ---- " + name + ".");
			
			//  name      name
			int pos = emotionNamePos(name);
			if(-1 != pos){
				//           
				Drawable drawable = getResources().getDrawable(emotionIconIds[pos]);
				drawable.setBounds(0, 10, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()+10);
				ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
				spannable.setSpan(span, leftAt, rightAt, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
			}
		}
		
		mEditText.setText(spannable);
		
		//         
		mEditText.requestFocus();
		mEditText.setSelection(content.length());
	}
	

여기서,
mEditText는 이모티콘과 웨이보 텍스트 내용을 표시하는 EditText 컨트롤입니다
emotionIconIds는 이모티콘 id의 int형 그룹입니다
emotionNames는 표정에 대응하는 문자(예를 들어 "[하하]")의 문자열 그룹입니다.
/**
	 *                  ,
	 *    ,     ,       -1;
	 * @param name
	 * @return
	 */
	private int emotionNamePos(String name){
		if(name == null) return -1;
		for(int i = 0; i < emotionNames.length; i++){
			if(name.equals(emotionNames[i])) return i;
		}
		return -1;
	}

좋은 웹페이지 즐겨찾기