public boolean addImages(Context context, Spannable spannable) { Pattern refImg = Pattern.compile("\\Q[img src=\\E([a-zA-Z0-9_]+?)\\Q/]\\E"); boolean hasChanges = false; Matcher matcher = refImg.matcher(spannable); while (matcher.find()) { boolean set = true; for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(), ImageSpan.class)) { if (spannable.getSpanStart(span) >= matcher.start() && spannable.getSpanEnd(span) <= matcher.end()) { spannable.removeSpan(span); } else { set = false; break; } } String resname = spannable.subSequence(matcher.start(1), matcher.end(1)).toString().trim(); int id = context.getResources().getIdentifier(resname, "drawable", context.getPackageName()); Drawable icon = context.getResources().getDrawable(id); // ,this.getTheme()); icon.setBounds(0, 0, tv_test1.getLineHeight(), tv_test1.getLineHeight()); if (set) { hasChanges = true; spannable.setSpan( new ImageSpan(icon, ImageSpan.ALIGN_BASELINE), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } return hasChanges; }
private void doScroll(MotionEvent event) { int xe = (int) event.getX(); int ye = (int) event.getY(); int txtHeight = textView.getLineHeight() * (textView.getLineCount() - 2); int xt = tvPos1.x + tvPos0.x - xe; int yt = tvPos1.y + tvPos0.y - ye; // if (xt < -textView.getRight()) { // xt = -textView.getRight() + 20; // } else if (xt > scrWidth) { xt = 0; // scrWidth - 20; // } if (yt < -textView.getBottom()) { yt = -textView.getBottom() + textView.getLineHeight(); } else if (yt > (txtHeight - scrHeight)) { yt = txtHeight - scrHeight; } else if (yt < 0) { yt = 0; } if (yt < 0) { yt = 0; } textView.scrollTo(xt, yt); tvPosSave.set(xt, yt); }
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int right = r - l; int height = b - t; for (int i = 0; i < getChildCount(); i++) { // Put all the children outside of the view, then use setPosition // to put the ones that need to be seen back. This will be where // we set the top and bottom of every view though. TextView view = (TextView) getChildAt(i); int viewWidth = view.getMeasuredWidth(); int viewHeight = view.getMeasuredHeight(); int textTop = (height / 2) - (viewHeight - ((view.getLineHeight() * view.getLineCount()) / 2)); view.layout(right, textTop, right + viewWidth, textTop + viewHeight); } setPosition(mDisplayedPage, 0, 0); mShadow.setBounds(0, 0, right, mShadowHeight); mBottomBar.setBounds(0, height - mBottomBarHeight, right, height); // Set up the fading edges mFadingEdgeLeft.setBounds(0, mShadowHeight, mFadingEdgeLength, height - mBottomBarHeight); mFadingEdgeRight.setBounds( right - mFadingEdgeLength, mShadowHeight, right, height - mBottomBarHeight); }
/** 对TextView进行伸缩处理 */ private void flexibleHeight() { isExpand = !isExpand; int textHeight = 0; float startDegree = 0.0f; float endDegree = 180.0f; if (isExpand) { // 如果是展开模式,那么取消最大行为maxLine的限制 textHeight = contentLine * mTextView.getLineHeight(); mTextView.setMaxLines(contentLine); } else { textHeight = mTextView.getLineHeight() * maxLine; endDegree = 0.0f; startDegree = 180.0f; } final LayoutParams mParam = (LayoutParams) mTextView.getLayoutParams(); // TextView的平移动画 ValueAnimator animator_textView = ValueAnimator.ofInt(mTextView.getHeight(), textHeight); animator_textView.addUpdateListener( new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { mParam.height = (Integer) animation.getAnimatedValue(); mTextView.setLayoutParams(mParam); } }); // imageView的旋转动画 ObjectAnimator animator_img = ObjectAnimator.ofFloat(mImageView, "rotation", startDegree, endDegree); AnimatorSet mAnimatorSets = new AnimatorSet(); mAnimatorSets.setDuration(500); mAnimatorSets.play(animator_img).with(animator_textView); mAnimatorSets.start(); mAnimatorSets.addListener( new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); // 动画结束之后,如果是非展开模式,则设置最大行数为maxLine if (!isExpand) { mTextView.setMaxLines(maxLine); } } }); }
private void drawTextCanvas(Canvas canvas, String text) { // TODO Auto-generated method stub // set color the background canvas.drawPaint(paint); // Setup textView like your normanlly would with your activity context TextView tv = new TextView(mContext); tv.setMaxWidth(300); // setup Text tv.setText(mText); // set Textcolor tv.setTextColor(Color.BLUE); tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13); // enable SetDrawing cache tv.setDrawingCacheEnabled(true); tv.measure( MeasureSpec.makeMeasureSpec(300, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(canvas.getHeight(), MeasureSpec.EXACTLY)); tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight()); Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.khung_xanh) .copy(Bitmap.Config.ARGB_8888, true); bm = Bitmap.createScaledBitmap( bm, tv.getMeasuredWidth() + 40, (int) ((int) tv.getLineCount() * (tv.getLineHeight() + 8)), true); canvas.drawBitmap(bm, 0, 0, paint); // draw bitmap from drawingcache to the canvas canvas.drawBitmap(tv.getDrawingCache(), 20, 20, paint); // disable drawing cache tv.setDrawingCacheEnabled(false); }
private void appendLog(String text) { Rect r = new Rect(); mLog.getDrawingRect(r); int maxLines = r.height() / mLog.getLineHeight() - 1; text = mLog.getText() + "\n" + text; // see how many lines we have int index = text.lastIndexOf('\n'); int count = 0; while (index > 0 && count <= maxLines) { count++; index = text.lastIndexOf('\n', index - 1); } // truncate to maxLines if (index > 0) { text = text.substring(index + 1); } mLog.setText(text); }
private void initViewAttribute() { mTextView = new TextView(this.getContext()); // 设置属性 mTextView.setText(text); mTextView.setTextColor(textColor); mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); int textHeight = mTextView.getLineHeight() * maxLine; LayoutParams mParams_txt = new LayoutParams(LayoutParams.MATCH_PARENT, textHeight); addView(mTextView, mParams_txt); mImageView = new ImageView(this.getContext()); mImageView.setImageDrawable(mIcon); LayoutParams mParams_img = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); mParams_img.topMargin = dp2px(DEFAULT_MARGIN_TOP); addView(mImageView, mParams_img); mImageView.setOnClickListener(this); this.setOnClickListener(this); this.post( new Runnable() { @Override public void run() { contentLine = mTextView.getLineCount(); if (contentLine <= maxLine) { mImageView.setVisibility(View.GONE); LayoutParams mParam = (LayoutParams) mTextView.getLayoutParams(); mParam.height = LayoutParams.WRAP_CONTENT; mTextView.setLayoutParams(mParam); ExpandTextView.this.setOnClickListener(null); } else { // 默认是非展开模式,那么设置最大行为maxLine mTextView.setMaxLines(maxLine); mTextView.setEllipsize(TruncateAt.END); mImageView.setVisibility(View.VISIBLE); } } }); }