آموزش درج عکس بین متن ( آموزش ALU جان )
سلام , به احتمال زیاد تاپیک ALU جان رو دیدید که آموزش داده بود چطور عکس رو بین متن درج کنیم اما کامل نبود و باید بخش زیادی رو خودمون مینوشتیم , منم که به چیزی سرم نمیشد با کلی خواهش کدو گرفتم D: ( دستشون درد نکنه )
بخشی از کد رو خودم نوشتم که قشنگ ضایعست از بس بد نوشتم :D , اینا رو ALU جان باید بیاد زحمتشو بکشه ویرایش کنه ( مخصوصا بخش اضافه کردن ImageView )
خوب اول ما متنمون رو از سرور دریافت میکنیم ( یا هر جا که دلمون میخواد ) و بعد اینو مینویسیم
processText(text, R.layout.image_style, R.layout.text_style);
متنمون یه چیزی باید مثل این باشه : [This is Text and This is Image [LoadImage:imagename.png
اینم بقیه کد
private void processText(String text) {
Pattern pattern = Pattern.compile("\\[LoadImage:(.*?)\\]");
Matcher matcher = pattern.matcher(text);
int offset = 0;
while (matcher.find()) {
try {
String textBeforImage = text.substring(offset, matcher.start());
offset += textBeforImage.length();
textBeforImage = textBeforImage.trim();
if (textBeforImage.length() != 0) {
addTextView(textBeforImage);
}
String ImageName = matcher.group(1).toString();
if (ImageName.length() != 0) {
addImageView(ImageName);
offset += text.substring(matcher.start(), matcher.end()).length();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
try {
if (offset != text.length()) {
String content = text.substring(offset).trim();
if (content.length() != 0) {
addTextView(content);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
اینم کد های addTextView و addImageView :
private void addTextView(String text){
View custom = G.layoutInflater.inflate(R.layout.text_style, null);
TextView tv = (TextView) custom.findViewById(R.id.myTxt);
tv.setText(text);
parent.addView(custom);
}
private void addImageView(String imageName){
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
lp.gravity=Gravity.CENTER;
lp.setMargins(0, 10, 0, 10);
ImageView img = new ImageView(this);
img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
img.setAdjustViewBounds(true);
img.setLayoutParams(lp);
final File imageFile = new File(G.DIR_FINAL + "/" + imageName);
if ( !imageFile.exists()) {
img.setImageBitmap(null);
String link = "http://example.com/imageFolder/" + imageName;
DownloadImage.addToDownloadList(link,img);
}else {
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
img.setImageBitmap(bitmap);
}
parent.addView(img);
}
من اینجا عکسو دانلود میکنم , از کد استاد برای دانلود استفاده میکنم , فقط حتما اینو بگم اگه میخواید دانلود کنید برای ست کردن عکس بعد تموم شدن دانلود تو onDownloadCompleteListener حتما اینجوری کد بنویسید :
Set<ImageView> imageViews = ActivityMain.imageMap.keySet();
for (ImageView imageView: imageViews) {
if (ActivityMain.imageMap.get(imageView).equals(filename)) {
if (imageView != null) {
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile(newPath, options);
imageView.setImageBitmap(bitmap);
}
}
}
تویه addToDownloadList ایمیج ویو رو به آرایه اضافه کنید :
ActivityMain.imageMap.put(imgLogo, filename);
آموزش کاملی نبود ولی کارتون رو راه میندازه ;)
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .