List of usage examples for android.widget ImageView measure
public final void measure(int widthMeasureSpec, int heightMeasureSpec)
This is called to find out how big a view should be.
From source file:Main.java
private static void updateImageViewAfterScaleTypeChange(ImageView imageView) { // enforcing imageView to update its internal bounds/matrix immediately imageView.measure(MeasureSpec.makeMeasureSpec(imageView.getMeasuredWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(imageView.getMeasuredHeight(), MeasureSpec.EXACTLY)); imageView.layout(imageView.getLeft(), imageView.getTop(), imageView.getRight(), imageView.getBottom()); }
From source file:org.dmfs.android.view.DrawablePagerTitleStrip.java
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final int widthMode = MeasureSpec.getMode(widthMeasureSpec); final int heightMode = MeasureSpec.getMode(heightMeasureSpec); final int widthSize = MeasureSpec.getSize(widthMeasureSpec); final int heightSize = MeasureSpec.getSize(heightMeasureSpec); if (widthMode != MeasureSpec.EXACTLY) { throw new IllegalStateException("Must measure with an exact width"); }/*from ww w .j a v a2 s .c om*/ int childHeight = heightSize; int minHeight = getMinHeight(); int padding = 0; padding = getPaddingTop() + getPaddingBottom(); childHeight -= padding; final int childWidthSpec = MeasureSpec.makeMeasureSpec((int) (widthSize * 0.8f), MeasureSpec.AT_MOST); final int childHeightSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.AT_MOST); for (ImageView iv : mImageViews) { iv.measure(childWidthSpec, childHeightSpec); } if (heightMode == MeasureSpec.EXACTLY) { setMeasuredDimension(widthSize, heightSize); } else { int textHeight = mImageViews[mImageViews.length / 2].getMeasuredHeight(); setMeasuredDimension(widthSize, Math.max(minHeight, textHeight + padding)); } }
From source file:org.dmfs.android.view.DrawablePagerTitleStrip.java
void updateImages(int currentItem, PagerAdapter adapter) { if (mImageViews == null || mImageViews.length == 0 || !(adapter instanceof IDrawableTitlePagerAdapter)) { // better throw an exception? return;//w ww. j av a 2 s . c o m } IDrawableTitlePagerAdapter dAdapter = (IDrawableTitlePagerAdapter) adapter; final int itemCount = adapter != null ? adapter.getCount() : 0; mUpdatingDrawables = true; int half = mImageViews.length / 2; for (int i = 0, l = mImageViews.length; i < l; ++i) { mImageViews[i].setImageDrawable( adapter != null && currentItem + (i - half) >= 0 && currentItem + (i - half) < itemCount ? dAdapter.getDrawableTitle(currentItem + (i - half)) : null); } // Measure everything final int width = getWidth() - getPaddingLeft() - getPaddingRight(); final int childHeight = getHeight() - getPaddingTop() - getPaddingBottom(); final int childWidthSpec = MeasureSpec.makeMeasureSpec((int) (width * 0.8f), MeasureSpec.AT_MOST); final int childHeightSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.AT_MOST); for (ImageView iv : mImageViews) { iv.measure(childWidthSpec, childHeightSpec); } mLastKnownCurrentPage = currentItem; if (!mUpdatingPositions) { updateDrawablePositions(currentItem, mLastKnownPositionOffset, false); } mUpdatingDrawables = false; }
From source file:com.keylesspalace.tusky.activity.MainActivity.java
private void fetchUserInfo() { SharedPreferences preferences = getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE); final String domain = preferences.getString("domain", null); String id = preferences.getString("loggedInAccountId", null); String username = preferences.getString("loggedInAccountUsername", null); if (id != null && username != null) { loggedInAccountId = id;/* w w w.j av a 2 s.co m*/ loggedInAccountUsername = username; } mastodonAPI.accountVerifyCredentials().enqueue(new Callback<Account>() { @Override public void onResponse(Call<Account> call, Response<Account> response) { if (!response.isSuccessful()) { onFetchUserInfoFailure(new Exception(response.message())); return; } Account me = response.body(); ImageView background = headerResult.getHeaderBackgroundView(); int backgroundWidth = background.getWidth(); int backgroundHeight = background.getHeight(); if (backgroundWidth == 0 || backgroundHeight == 0) { /* The header ImageView may not be layed out when the verify credentials call * returns so measure the dimensions and use those. */ background.measure(View.MeasureSpec.EXACTLY, View.MeasureSpec.EXACTLY); backgroundWidth = background.getMeasuredWidth(); backgroundHeight = background.getMeasuredHeight(); } Picasso.with(MainActivity.this).load(me.header).placeholder(R.drawable.account_header_missing) .resize(backgroundWidth, backgroundHeight).centerCrop().into(background); headerResult.addProfiles(new ProfileDrawerItem().withName(me.getDisplayName()) .withEmail(String.format("%s@%s", me.username, domain)).withIcon(me.avatar)); onFetchUserInfoSuccess(me.id, me.username); } @Override public void onFailure(Call<Account> call, Throwable t) { onFetchUserInfoFailure((Exception) t); } }); }
From source file:net.maa123.tatuky.MainActivity.java
private void onFetchUserInfoSuccess(Account me, String domain) { // Add the header image and avatar from the account, into the navigation drawer header. headerResult.clear();/*from ww w.j a v a 2 s . c o m*/ ImageView background = headerResult.getHeaderBackgroundView(); int backgroundWidth = background.getWidth(); int backgroundHeight = background.getHeight(); if (backgroundWidth == 0 || backgroundHeight == 0) { /* The header ImageView may not be layed out when the verify credentials call returns so * measure the dimensions and use those. */ background.measure(View.MeasureSpec.EXACTLY, View.MeasureSpec.EXACTLY); backgroundWidth = background.getMeasuredWidth(); backgroundHeight = background.getMeasuredHeight(); } background.setBackgroundColor(ContextCompat.getColor(this, R.color.window_background_dark)); Picasso.with(MainActivity.this).load(me.header).placeholder(R.drawable.account_header_default) .resize(backgroundWidth, backgroundHeight).centerCrop().into(background); headerResult.addProfiles(new ProfileDrawerItem().withName(me.getDisplayName()) .withEmail(String.format("%s@%s", me.username, domain)).withIcon(me.avatar)); // Show follow requests in the menu, if this is a locked account. if (me.locked) { PrimaryDrawerItem followRequestsItem = new PrimaryDrawerItem().withIdentifier(6) .withName(R.string.action_view_follow_requests).withSelectable(false) .withIcon(GoogleMaterial.Icon.gmd_person_add); drawer.addItemAtPosition(followRequestsItem, 3); } // Update the current login information. loggedInAccountId = me.id; loggedInAccountUsername = me.username; getPrivatePreferences().edit().putString("loggedInAccountId", loggedInAccountId) .putString("loggedInAccountUsername", loggedInAccountUsername) .putBoolean("loggedInAccountLocked", me.locked).apply(); }
From source file:com.fa.mastodon.activity.MainActivity.java
private void onFetchUserInfoSuccess(Account me, String domain) { // Add the header image and avatar from the account, into the navigation drawer header. headerResult.clear();/*from w w w . j a v a2 s .c o m*/ ImageView background = headerResult.getHeaderBackgroundView(); int backgroundWidth = background.getWidth(); int backgroundHeight = background.getHeight(); if (backgroundWidth == 0 || backgroundHeight == 0) { /* The header ImageView may not be layed out when the verify credentials call returns so * measure the dimensions and use those. */ background.measure(View.MeasureSpec.EXACTLY, View.MeasureSpec.EXACTLY); backgroundWidth = background.getMeasuredWidth(); backgroundHeight = background.getMeasuredHeight(); } background.setBackgroundColor(ContextCompat.getColor(this, R.color.window_background_dark)); if (backgroundWidth == 0 || backgroundHeight == 0) { } else { Picasso.with(MainActivity.this).load(me.header).placeholder(R.drawable.account_header_default) .resize(backgroundWidth, backgroundHeight).centerCrop().into(background); } headerResult.addProfiles(new ProfileDrawerItem().withName(me.getDisplayName()) .withEmail(String.format("%s@%s", me.username, domain)).withIcon(me.avatar)); // Show follow requests in the menu, if this is a locked account. if (me.locked) { PrimaryDrawerItem followRequestsItem = new PrimaryDrawerItem().withIdentifier(6) .withName(R.string.action_view_follow_requests).withSelectable(false) .withIcon(GoogleMaterial.Icon.gmd_person_add); drawer.addItemAtPosition(followRequestsItem, 3); } // Update the current login information. loggedInAccountId = me.id; loggedInAccountUsername = me.username; getPrivatePreferences().edit().putString("loggedInAccountId", loggedInAccountId) .putString("loggedInAccountUsername", loggedInAccountUsername) .putBoolean("loggedInAccountLocked", me.locked).apply(); }
From source file:github.daneren2005.dsub.fragments.SelectDirectoryFragment.java
private void setupTextDisplay(final View header) { final TextView titleView = (TextView) header.findViewById(R.id.select_album_title); if (playlistName != null) { titleView.setText(playlistName); } else if (podcastName != null) { titleView.setText(podcastName);/*from www .j av a 2s. c om*/ titleView.setPadding(0, 6, 4, 8); } else if (name != null) { titleView.setText(name); if (artistInfo != null) { titleView.setPadding(0, 6, 4, 8); } } else if (share != null) { titleView.setVisibility(View.GONE); } int songCount = 0; Set<String> artists = new HashSet<String>(); Set<Integer> years = new HashSet<Integer>(); Integer totalDuration = 0; for (Entry entry : entries) { if (!entry.isDirectory()) { songCount++; if (entry.getArtist() != null) { artists.add(entry.getArtist()); } if (entry.getYear() != null) { years.add(entry.getYear()); } Integer duration = entry.getDuration(); if (duration != null) { totalDuration += duration; } } } final TextView artistView = (TextView) header.findViewById(R.id.select_album_artist); if (podcastDescription != null || artistInfo != null) { artistView.setVisibility(View.VISIBLE); String text = podcastDescription != null ? podcastDescription : artistInfo.getBiography(); Spanned spanned = null; if (text != null) { spanned = Html.fromHtml(text); } artistView.setText(spanned); artistView.setSingleLine(false); final int minLines = context.getResources().getInteger(R.integer.TextDescriptionLength); artistView.setLines(minLines); artistView.setTextAppearance(context, android.R.style.TextAppearance_Small); final Spanned spannedText = spanned; artistView.setOnClickListener(new View.OnClickListener() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onClick(View v) { if (artistView.getMaxLines() == minLines) { // Use LeadingMarginSpan2 to try to make text flow around image Display display = context.getWindowManager().getDefaultDisplay(); ImageView coverArtView = (ImageView) header.findViewById(R.id.select_album_art); coverArtView.measure(display.getWidth(), display.getHeight()); int height, width; ViewGroup.MarginLayoutParams vlp = (ViewGroup.MarginLayoutParams) coverArtView .getLayoutParams(); if (coverArtView.getDrawable() != null) { height = coverArtView.getMeasuredHeight() + coverArtView.getPaddingBottom(); width = coverArtView.getWidth() + coverArtView.getPaddingRight(); } else { height = coverArtView.getHeight(); width = coverArtView.getWidth() + coverArtView.getPaddingRight(); } float textLineHeight = artistView.getPaint().getTextSize(); int lines = (int) Math.ceil(height / textLineHeight); SpannableString ss = new SpannableString(spannedText); ss.setSpan(new MyLeadingMarginSpan2(lines, width), 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); View linearLayout = header.findViewById(R.id.select_album_text_layout); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) linearLayout .getLayoutParams(); int[] rules = params.getRules(); rules[RelativeLayout.RIGHT_OF] = 0; params.leftMargin = vlp.rightMargin; artistView.setText(ss); artistView.setMaxLines(100); vlp = (ViewGroup.MarginLayoutParams) titleView.getLayoutParams(); vlp.leftMargin = width; } else { artistView.setMaxLines(minLines); } } }); artistView.setMovementMethod(LinkMovementMethod.getInstance()); } else if (topTracks) { artistView.setText(R.string.menu_top_tracks); artistView.setVisibility(View.VISIBLE); } else if (showAll) { artistView.setText(R.string.menu_show_all); artistView.setVisibility(View.VISIBLE); } else if (artists.size() == 1) { String artistText = artists.iterator().next(); if (years.size() == 1) { artistText += " - " + years.iterator().next(); } artistView.setText(artistText); artistView.setVisibility(View.VISIBLE); } else { artistView.setVisibility(View.GONE); } TextView songCountView = (TextView) header.findViewById(R.id.select_album_song_count); TextView songLengthView = (TextView) header.findViewById(R.id.select_album_song_length); if (podcastDescription != null || artistInfo != null) { songCountView.setVisibility(View.GONE); songLengthView.setVisibility(View.GONE); } else { String s = context.getResources().getQuantityString(R.plurals.select_album_n_songs, songCount, songCount); songCountView.setText(s.toUpperCase()); songLengthView.setText(Util.formatDuration(totalDuration)); } }
From source file:github.popeen.dsub.fragments.SelectDirectoryFragment.java
private void setupTextDisplay(final View header) { final TextView titleView = (TextView) header.findViewById(R.id.select_album_title); if (playlistName != null) { titleView.setText(playlistName); } else if (podcastName != null) { Collections.reverse(entries); titleView.setText(podcastName);//from www. java2 s. com titleView.setPadding(0, 6, 4, 8); } else if (name != null) { titleView.setText(name); if (artistInfo != null) { titleView.setPadding(0, 6, 4, 8); } } else if (share != null) { titleView.setVisibility(View.GONE); } int songCount = 0; Set<String> artists = new HashSet<String>(); Set<Integer> years = new HashSet<Integer>(); totalDuration = 0; for (Entry entry : entries) { if (!entry.isDirectory()) { songCount++; if (entry.getArtist() != null) { artists.add(entry.getArtist()); } if (entry.getYear() != null) { years.add(entry.getYear()); } Integer duration = entry.getDuration(); if (duration != null) { totalDuration += duration; } } } String artistName = ""; bookDescription = "Could not collect any info about the book at this time"; try { artistName = artists.iterator().next(); String endpoint = "getBookDirectory"; if (Util.isTagBrowsing(context)) { endpoint = "getBook"; } SharedPreferences prefs = Util.getPreferences(context); String url = Util.getRestUrl(context, endpoint) + "&id=" + directory.getId() + "&f=json"; Log.w("GetInfo", url); String artist, title; int year = 0; artist = title = ""; try { artist = artists.iterator().next(); } catch (Exception e) { Log.w("GetInfoArtist", e.toString()); } try { title = titleView.getText().toString(); } catch (Exception e) { Log.w("GetInfoTitle", e.toString()); } try { year = years.iterator().next(); } catch (Exception e) { Log.w("GetInfoYear", e.toString()); } BookInfoAPIParams params = new BookInfoAPIParams(url, artist, title, year); bookInfo = new BookInfoAPI(context).execute(params).get(); bookDescription = bookInfo[0]; bookReader = bookInfo[1]; } catch (Exception e) { Log.w("GetInfoError", e.toString()); } if (bookDescription.equals("noInfo")) { bookDescription = "The server has no description for this book"; } final TextView artistView = (TextView) header.findViewById(R.id.select_album_artist); if (podcastDescription != null || artistInfo != null || bookDescription != null) { artistView.setVisibility(View.VISIBLE); String text = ""; if (bookDescription != null) { text = bookDescription; } if (podcastDescription != null) { text = podcastDescription; } if (artistInfo != null) { text = artistInfo.getBiography(); } Spanned spanned = null; if (text != null) { String newText = ""; try { if (!artistName.equals("")) { newText += "<b>" + context.getResources().getString(R.string.main_artist) + "</b>: " + artistName + "<br/>"; } } catch (Exception e) { } try { if (totalDuration > 0) { newText += "<b>" + context.getResources().getString(R.string.album_book_reader) + "</b>: " + bookReader + "<br/>"; } } catch (Exception e) { } try { if (totalDuration > 0) { newText += "<b>" + context.getResources().getString(R.string.album_book_length) + "</b>: " + Util.formatDuration(totalDuration) + "<br/>"; } } catch (Exception e) { } try { newText += text + "<br/>"; } catch (Exception e) { } spanned = Html.fromHtml(newText); } artistView.setText(spanned); artistView.setSingleLine(false); final int minLines = context.getResources().getInteger(R.integer.TextDescriptionLength); artistView.setLines(minLines); artistView.setTextAppearance(context, android.R.style.TextAppearance_Small); final Spanned spannedText = spanned; artistView.setOnClickListener(new View.OnClickListener() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onClick(View v) { if (artistView.getMaxLines() == minLines) { // Use LeadingMarginSpan2 to try to make text flow around image Display display = context.getWindowManager().getDefaultDisplay(); ImageView coverArtView = (ImageView) header.findViewById(R.id.select_album_art); coverArtView.measure(display.getWidth(), display.getHeight()); int height, width; ViewGroup.MarginLayoutParams vlp = (ViewGroup.MarginLayoutParams) coverArtView .getLayoutParams(); if (coverArtView.getDrawable() != null) { height = coverArtView.getMeasuredHeight() + coverArtView.getPaddingBottom(); width = coverArtView.getWidth() + coverArtView.getPaddingRight(); } else { height = coverArtView.getHeight(); width = coverArtView.getWidth() + coverArtView.getPaddingRight(); } float textLineHeight = artistView.getPaint().getTextSize(); int lines = (int) Math.ceil(height / textLineHeight) + 1; SpannableString ss = new SpannableString(spannedText); ss.setSpan(new MyLeadingMarginSpan2(lines, width), 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); View linearLayout = header.findViewById(R.id.select_album_text_layout); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) linearLayout .getLayoutParams(); int[] rules = params.getRules(); rules[RelativeLayout.RIGHT_OF] = 0; params.leftMargin = vlp.rightMargin; artistView.setText(ss); artistView.setMaxLines(100); vlp = (ViewGroup.MarginLayoutParams) titleView.getLayoutParams(); vlp.leftMargin = width; } else { artistView.setMaxLines(minLines); } } }); artistView.setMovementMethod(LinkMovementMethod.getInstance()); } else if (topTracks) { artistView.setText(R.string.menu_top_tracks); artistView.setVisibility(View.VISIBLE); } else if (showAll) { artistView.setText(R.string.menu_show_all); artistView.setVisibility(View.VISIBLE); } else if (artists.size() == 1) { String artistText = artists.iterator().next(); if (years.size() == 1) { artistText += " - " + years.iterator().next(); } artistView.setText(artistText); artistView.setVisibility(View.VISIBLE); } else { artistView.setVisibility(View.GONE); } TextView songCountView = (TextView) header.findViewById(R.id.select_album_song_count); TextView songLengthView = (TextView) header.findViewById(R.id.select_album_song_length); if (podcastDescription != null || artistInfo != null) { songCountView.setVisibility(View.GONE); songLengthView.setVisibility(View.GONE); } else { String s = context.getResources().getQuantityString(R.plurals.select_album_n_songs, songCount, songCount); songCountView.setVisibility(View.GONE); songLengthView.setVisibility(View.GONE); } }