本篇文章為大家展示了Android開(kāi)發(fā)中如何實(shí)現(xiàn)一個(gè)圖片中疊加文字功能,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
陽(yáng)曲網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)公司自2013年起到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專(zhuān)注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
效果圖:
功能:
1.用戶(hù)自由輸入內(nèi)容,可手動(dòng)換行,并且行滿(mǎn)也會(huì)自動(dòng)換行。
2.可拖動(dòng)改變圖片中文本位置(文字不會(huì)超出圖片區(qū)域)。
3.點(diǎn)擊“生成圖片”按鈕之后,生成一張帶有文字的圖片文件。
代碼不多,直接全部貼上了:
Activity:
/** * 將文字寫(xiě)在圖片中(截圖方式),支持拖動(dòng)文字。
* 說(shuō)明:很明顯,截圖方式會(huì)降低圖片的質(zhì)量。如果需要保持圖片質(zhì)量可以使用canvas的方式,將文字直接繪制在圖片之上(不過(guò),使用此方式要實(shí)現(xiàn)文字拖動(dòng)較為復(fù)雜)。 */ public class MainActivity extends AppCompatActivity { //圖片組件 private ImageView imageView; //位于圖片中的文本組件 private TextView tvInImage; //圖片和文本的父組件 private View containerView; //父組件的尺寸 private float imageWidth, imageHeight, imagePositionX, imagePositionY; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.image_with_text); imageView = (ImageView) findViewById(R.id.writeText_img); EditText editText = (EditText) findViewById(R.id.writeText_et); tvInImage = (TextView) findViewById(R.id.writeText_image_tv); containerView = findViewById(R.id.writeText_img_rl); imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this); imagePositionX = imageView.getX(); imagePositionY = imageView.getY(); imageWidth = imageView.getWidth(); imageHeight = imageView.getHeight(); //設(shè)置文本大小 tvInImage.setMaxWidth((int) imageWidth); } }); imageView.setImageBitmap(getScaledBitmap(R.mipmap.test_img)); //輸入框 editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (s.toString().equals("")) { tvInImage.setVisibility(View.INVISIBLE); } else { tvInImage.setVisibility(View.VISIBLE); tvInImage.setText(s); } } @Override public void afterTextChanged(Editable s) { } }); final GestureDetector gestureDetector = new GestureDetector(this, new SimpleGestureListenerImpl()); //移動(dòng) tvInImage.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { gestureDetector.onTouchEvent(event); return true; } }); } //確認(rèn),生成圖片 public void confirm(View view) { Bitmap bm = loadBitmapFromView(containerView); String filePath = Environment.getExternalStorageDirectory() + File.separator + "image_with_text.jpg"; try { bm.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(filePath)); Toast.makeText(this, "圖片已保存至:SD卡根目錄/image_with_text.jpg", Toast.LENGTH_LONG).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } } //以圖片形式獲取View顯示的內(nèi)容(類(lèi)似于截圖) public static Bitmap loadBitmapFromView(View view) { Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); return bitmap; } private int count = 0; //tvInImage的x方向和y方向移動(dòng)量 private float mDx, mDy; //移動(dòng) private class SimpleGestureListenerImpl extends GestureDetector.SimpleOnGestureListener { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { //向右移動(dòng)時(shí),distanceX為負(fù);向左移動(dòng)時(shí),distanceX為正 //向下移動(dòng)時(shí),distanceY為負(fù);向上移動(dòng)時(shí),distanceY為正 count++; mDx -= distanceX; mDy -= distanceY; //邊界檢查 mDx = calPosition(imagePositionX - tvInImage.getX(), imagePositionX + imageWidth - (tvInImage.getX() + tvInImage.getWidth()), mDx); mDy = calPosition(imagePositionY - tvInImage.getY(), imagePositionY + imageHeight - (tvInImage.getY() + tvInImage.getHeight()), mDy); //控制刷新頻率 if (count % 5 == 0) { tvInImage.setX(tvInImage.getX() + mDx); tvInImage.setY(tvInImage.getY() + mDy); } return true; } } //計(jì)算正確的顯示位置(不能超出邊界) private float calPosition(float min, float max, float current) { if (current < min) { return min; } if (current > max) { return max; } return current; } //獲取壓縮后的bitmap private Bitmap getScaledBitmap(int resId) { BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), resId, opt); opt.inSampleSize = Utility.calculateInSampleSize(opt, 600, 800); opt.inJustDecodeBounds = false; return BitmapFactory.decodeResource(getResources(), resId, opt); } }
一個(gè)工具類(lèi):
public class Utility { //計(jì)算 inSampleSize 值,壓縮圖片 public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize; } }
布局文件:
<?xml version="1.0" encoding="utf-8"?>
上述內(nèi)容就是Android開(kāi)發(fā)中如何實(shí)現(xiàn)一個(gè)圖片中疊加文字功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。