Programaticaly set ImageView position in “dp”?

I've read:

How to set margin of ImageView using code, not xml

and the comment to use "You can scale the px value using context.getResources().getDisplayMetrics().density "

Thing is, I have scale representing some values in colors, and need to point cursor to certain value.

As everything is counted in "dip"s how can I send dip grammatically, not px?

eg:

0__T_h_i_s__i_s_m_y_s_c_a_l_e_______300dp

        ^  - My cursor pointing to 100dp (LeftMargin = 100dp)

I'm using this line of code to set it up:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(leftMargin, 0, 0, 0);
BMIcursor.setLayoutParams(lp);

Tnx.

Answers


The left margin in pixels can be computed like this:

leftMarginPx = leftMarginDp * context.getResources().getDisplayMetrics().density;

So with your code:

    leftMarginDp = 100 // 100dp;
    leftMargin = leftMarginDp * context.getResources().getDisplayMetrics().density;
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    lp.setMargins(leftMargin, 0, 0, 0);
    BMIcursor.setLayoutParams(lp);

Need Your Help

Keep libgdx camera inside boundaries when panning and zooming

android libgdx

I am developing a game for Android using LibGDX. I have added pinch zoom and pan. My issue is how to keep from going outside of the play area. As it is, you can pan outside of the play area into

Auto-updated iOS application for enterprise distribution

iphone ios mobile cross-platform

Is it possible to make an iOS application for enterprise distribution that updates itself transparently? Saying 'updates' I mean completely updating its logic.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.